title: Apache HTTPClient Streaming HTTP POST Request?
body: <p>I'm trying to build a "full-duplex" HTTP streaming request using <a href="http://hc.apache.org/httpcomponents-client-ga/" rel="noreferrer">Apache HTTPClient</a>.</p>
  
  <p>In my first attempt, I tried using the following request code:</p>
  
  <pre><code>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); // &lt;-- 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());
  </code></pre>
  
  <p>The request hangs at the line indicated above. It seems that the code is trying to send the entire request before it gets the response. In retrospect, this makes sense. However, it's not what I was hoping for. :)</p>
  
  <p>Instead, I was hoping to send the request headers with <code>Transfer-Encoding: chunked</code>, receive a response header of <code>HTTP/1.1 200 OK</code> with a <code>Transfer-Encoding: chunked</code> header of its own, and then I'd have a full-duplex streaming HTTP connection to work with.</p>
  
  <p>Happily, my HTTPClient has another NIO-based asynchronous client with good usage examples (like <a href="http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/examples/org/apache/http/examples/nio/NHttpClient.java" rel="noreferrer">this one</a>). My questions are:</p>
  
  <ol>
  <li>Is my interpretation of the synchronous HTTPClient behavior correct? Or is there something I can do to continue using the (simpler) synchronous HTTPClient in the manner I described?</li>
  <li>Does the NIO-based client wait to send the whole request before seeking a response? Or will I be able to send the request incrementally and receive the response incrementally at the same time?</li>
  </ol>
  
  <p>If HTTPClient will not support this modality, is there another HTTP client library that will? Or should I be planning to write a (minimal) HTTP client to support this modality?</p>