title: When POSTing a MultipartEntity using Apache HttpClient, ByteArrayBody works but InputStreamBody throws NoHttpResponseException
body: <p>I am trying to POST some data taken from an InputStream. I was hoping to avoid having to gather all the data into a byte array first, and instead stream the data, to save memory (I am POSTing data of about 10MB).</p>
  
  <p>If I run the following:</p>
  
  <pre><code>DefaultHttpClient httpclient = new DefaultHttpClient();
  HttpPost httpPost = new HttpPost(target_url);
  MultipartEntity entity = new MultipartEntity();
  entity.addPart("data", new InputStreamBody(new ByteArrayInputStream(new byte[1]), 
      filename));
  httpPost.setEntity(entity);
  httpclient.execute(httpPost);
  </code></pre>
  
  <p>then I get:</p>
  
  <pre><code>org.apache.http.NoHttpResponseException: The target server failed to respond
  at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:95)
  at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
  at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
  at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
  at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
  at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
  at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
  at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
  at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712)
  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
      [my code starts here]
  </code></pre>
  
  <p>However, if I change one line above, replacing the InputStreamBody with the equivalent ByteArrayBody, then all is well:</p>
  
  <pre><code>entity.addPart("data", new ByteArrayBody(new byte[1], filename));
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>(and would InputStreamBody actually stream the data, or am I wasting my time?)</p>
  
  <p>I am using HttpClient-4.2.1</p>