Take the 2-minute tour ×

I have a loop over a bunch of URLs, for each one I'm doing the following:

private String doQuery(String url) {

  HttpGet httpGet = new HttpGet(url);
  setDefaultHeaders(httpGet); // static method
  HttpResponse response = httpClient.execute(httpGet);   // httpClient instantiated in constructor

  int rc = response.getStatusLine().getStatusCode();

  if (rc != 200) {
    // some stuff...

  HttpEntity entity = response.getEntity();

  if (entity == null) {
    // some stuff...

  // process the entity, get input stream etc


The first query is fine, the second throws this exception:

Exception in thread "main" java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated. Make sure to release the connection before allocating another one. at org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:199) at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:173)......

This is just a simple single-threaded app. How can I release this connection?

share|improve this question

7 Answers 7

The recommended way, by Httpcomponents 4.1, is to close connection and release any underlying resources:


where HttpEntity passed is a response entity.

share|improve this answer
This is the right answer for 4.1. The one from Richard H one does not work for me. –  Alexandre Martins Dec 8 '11 at 21:50
I am facing the same problem. This solution works but does not cover all the possible cases. I presume httpClient is being reused. If some requests fail, as you would expect with HTTP, its unclear how to free the allocated resources. Even following the example that Apache provides with multiple different exception handlers, I still face the same issue :( –  Rafael Feb 3 '12 at 7:45
@Rafael you can use HttpRequestBase.releaseConnection() or HttpUriRequest.abort() if exception is thrown before getting handle of response entity. –  Yadu Sep 28 '12 at 21:29
There is no consume method in the EntityUtils class developer.android.com/reference/org/apache/http/util/… –  jb11 Feb 18 at 22:40

This seems to work great :

      if( response.getEntity() != null ) {

And don't forget to consume the entity even if you didn't open its content. For instance, you expect a HTTP_OK status from the response and don't get it, you still have to consume the entity !

share|improve this answer
consumeContent() is now (4.1.2) deprecated. A one liner instead: EntityUtils.consume(response.getEntity()); –  Gili Nachum Nov 19 '12 at 13:33
up vote 12 down vote accepted

To answer my own question: to release the connection (and any other resources associated with the request) you must close the InputStream returned by the HttpEntity:

InputStream is = entity.getContent();

.... process the input stream ....

is.close();       // releases all resources

From the docs

share|improve this answer
I don't think so, I do that and I'm getting "Invalid use of SingleClientConnManager: connection still allocated". it might be done via ClientConnectionManager that I can get from httpClient, but it doesn't seem right –  lisak Aug 17 '11 at 14:40
neither do I. are you reusing HTTPClient? how do you ensure it releases resources when requests do fail? –  Rafael Feb 3 '12 at 7:46
I can confirm that by closing the InputStream I see the debug message: org.apache.http.impl.conn.BasicClientConnectionManager(18314): Releasing connection –  Jona Nov 7 '13 at 3:29

Since version 4.2, they introduced a much more convenience method that simplifies connection release: HttpRequestBase.releaseConnection()

share|improve this answer

If the response is not to be consumed, then the request can be aborted using the code below:

        // Low level resources should be released before initiating a new request
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Do not need the rest

Reference: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e143

Apache HttpClient Version: 4.1.3



share|improve this answer

I've got this problem when I use HttpClient in Multithread envirnoment (Servlets). One servlet still holds connection and another one want to get connection.


version 4.0 use ThreadSafeClientConnManager

version 4.2 use PoolingClientConnectionManager

and set this two setters:

share|improve this answer

HTTP HEAD requests must be treated slightly differently because response.getEntity() is null. Instead, you must capture the HttpContext passed into HttpClient.execute() and retrieve the connection parameter to close it (in HttpComponents 4.1.X anyway).

HttpRequest httpRqst = new HttpHead( uri );
HttpContext httpContext = httpFactory.createContext();
HttpResponse httpResp = httpClient.execute( httpRqst, httpContext );


// Close when finished
HttpEntity entity = httpResp.getEntity();
if( null != entity )
  // Handles standard 'GET' case
  EntityUtils.consume( entity );
else {
  ConnectionReleaseTrigger  conn =
      (ConnectionReleaseTrigger) httpContext.getAttribute( ExecutionContext.HTTP_CONNECTION );
  // Handles 'HEAD' where entity is not returned
  if( null != conn )

HttpComponents 4.2.X added a releaseConnection() to HttpRequestBase to make this easier.

share|improve this answer

Your Answer


