hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Julien <tjul...@limewire.com>
Subject how do I close connections on HttpClient 4.x?
Date Thu, 27 Dec 2007 21:00:57 GMT

I am in the process of migrating from HttpClient 2.x to HttpClient 4.x. 
(This is for Limewire)

Is there a migration guide anywhere that helps you go from the "old" 
stack to the new one based on HttpCore?

I think I've banged my way through it pretty well but I'm struggling 
with how to close connections (or at least tell the connection manager 
that i'm finished - close if not maintaining persistent connection).  On 
the old stack I could do this:

org.apache.commons.httpclient.methods.GetMethod get = ....
// make a connection

Under the new stack I'm not totally clear.

I've seen the example at:

But based on that, the only method available to me on the request object 
is abort() - which seems like it is only to be used when an unexpected 
error occurs (RuntimeException).

In the finally block of that example it says all you have to do to close 
a connection is call response.getEntity().getContent().close().  Other 
places seem to indicate I could alternatively call 

Is close()'ing or consumeContent()'ing an HttpEntity enough to release a 

Are there other ways (What if I don't have an HttpEntity)?  It's a neat 
trick that "closing" the HttpEntity has the side affect of releasing the 
connection -  but it's a little confusing if thats the best practice for 
releasing a connection.

I also see that *if* I could get a reference to 
org.apache.http.HttpConnection then I could call close().
Or if I could get org.apache.http.conn.ManagedClientConnection I could 
call close() or releaseConnection().  However, those aren't super easy 
to get a hold of:

org.apache.http.client.HttpClient client = new DefaultHttpClient();
org.apache.http.client.methods.HttpGet get = new HttpGet(myURL);
// client.execute(get), do some work
// ...

// now lets close the connection:
org.apache.http.HttpHost host = new HttpHost(get.getURI());
org.apache.http.conn.HttpRoute route = new HttpRoute(host);
org.apache.http.conn.ManagedClientConnection connection = 



-Tim Julien

To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org

View raw message