title: HttpClient response handler always returns closed stream
body: <p>I'm new to Java development so please bear with me. Also, I hope I'm not the champion of <a href="http://www.urbandictionary.com/define.php?term=tl;dr" rel="nofollow noreferrer">tl;dr</a> :).</p>
  
  <p>I'm using <a href="http://hc.apache.org/downloads.cgi" rel="nofollow noreferrer">HttpClient</a> to make requests over Http (duh!) and I'd gotten it to work for a simple servlet that receives an URL as a query string parameter. I realized that my code could use some refactoring, so I decided to make my own <code>HttpResponseHandler</code>, to clean up the code, make it reusable and improve exception handling.</p>
  
  <p>I currently have something like this:</p>
  
  <pre><code>public class HttpResponseHandler implements ResponseHandler&lt;InputStream&gt;{
  
      public InputStream handleResponse(HttpResponse response)
              throws ClientProtocolException, IOException {
  
          int statusCode = response.getStatusLine().getStatusCode();
          InputStream in = null;
  
          if (statusCode != HttpStatus.SC_OK) {
              throw new HttpResponseException(statusCode, null);
          } else {
              HttpEntity entity = response.getEntity();
              if (entity != null) {
                  in = entity.getContent();
                  // This works
                  // for (int i;(i = in.read()) &gt;= 0;) System.out.print((char)i); 
              }
          }
          return in;
      }
  }
  </code></pre>
  
  <p>And in the method where I make the actual request:</p>
  
  <pre><code>HttpClient httpclient = new DefaultHttpClient();
  HttpGet httpget = new HttpGet(target);
  ResponseHandler&lt;InputStream&gt; httpResponseHandler = new HttpResponseHandler();
  try {
      InputStream in = httpclient.execute(httpget, httpResponseHandler);
      // This doesn't work
      // for (int i;(i = in.read()) &gt;= 0;) System.out.print((char)i);     
      return in;
  } catch (HttpResponseException e) {
  throw new HttpResponseException(e.getStatusCode(), null);
  }
  </code></pre>
  
  <p>The problem is that the input stream returned from the handler is <em>closed</em>. I don't have any idea why, but I've checked it with the prints in my code (and no, I haven't used them both at the same time :). While the first print works, the other one gives a closed stream error. </p>
  
  <p>I need <code>InputStreams</code>, because all my other methods expect an <code>InputStream</code> and not a <code>String</code>. Also, I want to be able to retrieve images (or maybe other types of files), not just text files.</p>
  
  <p>I can work around this pretty easily by giving up on the response handler (I have a working implementation that doesn't use it), but I'm pretty curious about the following:</p>
  
  <ol>
  <li><em>Why</em> does it do what it does?</li>
  <li>How do I open the stream, if <em>something</em> closes it?</li>
  <li>What's the right way to do this, anyway :)?</li>
  </ol>
  
  <p>I've checked the docs and I couldn't find anything useful regarding this issue. To save you a bit of Googling, here's the <a href="http://hc.apache.org/httpcomponents-client-4.0.1/httpclient/apidocs/index.html" rel="nofollow noreferrer">Javadoc</a> and here's the <a href="http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/fundamentals.html" rel="nofollow noreferrer">HttpClient tutorial</a> (Section 1.1.8 - Response handlers).</p>
  
  <p>Thanks,<br>
  Alex</p>