Apache HTTPClientを使用して「全二重」HTTP ストリーミング リクエストを構築しようとしています。
最初の試みでは、次のリクエスト コードを使用してみました。
URL url=new URL(/* code goes here */);
HttpPost request=new HttpPost(url.toString());
request.addHeader("Connection", "close");
PipedOutputStream requestOutput=new PipedOutputStream();
PipedInputStream requestInput=new PipedInputStream(requestOutput, DEFAULT_PIPE_SIZE);
ContentType requestContentType=getContentType();
InputStreamEntity requestEntity=new InputStreamEntity(requestInput, -1, requestContentType);
request.setEntity(requestEntity);
HttpEntity responseEntity=null;
HttpResponse response=getHttpClient().execute(request); // <-- Hanging here
try {
if(response.getStatusLine().getStatusCode() != 200)
throw new IOException("Unexpected status code: "+response.getStatusLine().getStatusCode());
responseEntity = response.getEntity();
}
finally {
if(responseEntity == null)
request.abort();
}
InputStream responseInput=responseEntity.getContent();
ContentType responseContentType;
if(responseEntity.getContentType() != null)
responseContentType = ContentType.parse(responseEntity.getContentType().getValue());
else
responseContentType = DEFAULT_CONTENT_TYPE;
Reader responseStream=decode(responseInput, responseContentType);
Writer requestStream=encode(requestOutput, getContentType());
要求は、上記の行でハングします。コードは、応答を取得する前に要求全体を送信しようとしているようです。振り返ってみると、これは理にかなっています。しかし、それは私が望んでいたものではありません。:)
代わりに、 でリクエスト ヘッダーを送信し、独自のヘッダーでのTransfer-Encoding: chunked
レスポンス ヘッダーを受信してから、全二重ストリーミング HTTP 接続を使用することを望んでいました。HTTP/1.1 200 OK
Transfer-Encoding: chunked
幸いなことに、私の HTTPClient には別の NIO ベースの非同期クライアントがあり、適切な使用例 (このようなもの) があります。私の質問は次のとおりです。
- 同期 HTTPClient の動作に対する私の解釈は正しいですか? または、説明した方法で (より単純な) 同期 HTTPClient を引き続き使用するためにできることはありますか?
- NIO ベースのクライアントは、応答を求める前に要求全体を送信するのを待ちますか? または、リクエストを段階的に送信し、同時に応答を段階的に受信することはできますか?
HTTPClient がこのモダリティをサポートしない場合、別の HTTP クライアント ライブラリはありますか? または、このモダリティをサポートするために (最小限の) HTTP クライアントを作成することを計画する必要がありますか?