You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Brijesh Deo <bd...@SonicWALL.com> on 2009/04/01 11:54:50 UTC

How to release connection with HttpClient 4.0 api ?

Hi,

 

I am trying to port my earlier code which used HttpClient 3.1 to now use
HttpClient 4.0. I am trying to find out a corresponding method with 4.0
to gracefully release a connection as it was being done with the 3.1
api.

 

With HC 3.1:

GetMethod method = new GetMethod("/someURL");

//execute the method using the HttpClient instance

//and then finally release the connection as shown below.

method.releaseConnection();

 

 

With HC 4.0:

HttpGet httpGet = new HttpGet("/someURL");

//execute the method using the HttpClient instance

HttpResponse response = httpClient.execute(httpget);

 

How to release the connection gracefully?

I don't want to call the abort() method on the httpGet instance.

 

Is it sufficient to call the consumeContect() to release a connection
gracefully?

HttpEntity entity = response.getEntity();

If (entity != null) {

            entity.consumeContent();

}

 

 

Please let me know if there is a better and more efficient way to do it.
I have also tried to get the underlying inputstream from the response
entity and calling a close() on that but it's resulting in a
SocketException from other parts in my code.

 

Thanks,

Brijesh

 

 


Re: How to release connection with HttpClient 4.0 api ?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2009-04-01 at 15:24 +0530, Brijesh Deo wrote:
> Hi,
> 
>  
> 
> I am trying to port my earlier code which used HttpClient 3.1 to now use
> HttpClient 4.0. I am trying to find out a corresponding method with 4.0
> to gracefully release a connection as it was being done with the 3.1
> api.
> 
>  
> 
> With HC 3.1:
> 
> GetMethod method = new GetMethod("/someURL");
> 
> //execute the method using the HttpClient instance
> 
> //and then finally release the connection as shown below.
> 
> method.releaseConnection();
> 
>  
> 
> 
> 
> With HC 4.0:
> 
> HttpGet httpGet = new HttpGet("/someURL");
> 
> //execute the method using the HttpClient instance
> 
> HttpResponse response = httpClient.execute(httpget);
> 
>  
> 
> How to release the connection gracefully?
> 
> I don't want to call the abort() method on the httpGet instance.
> 
>  
> 
> Is it sufficient to call the consumeContect() to release a connection
> gracefully?
> 
> HttpEntity entity = response.getEntity();
> 
> If (entity != null) {
> 
>             entity.consumeContent();
> 
> }
> 
>  
> 
> 
> 
> Please let me know if there is a better and more efficient way to do it.

This is indeed the recommended way. For details, see the 'Ensuring
release of low level resources' section of the tutorial

http://wiki.apache.org/HttpComponents/HttpClientTutorial


> I have also tried to get the underlying inputstream from the response
> entity and calling a close() on that but it's resulting in a
> SocketException from other parts in my code.
> 

This should also work. What kind of SocketException are you getting? 

Oleg




>  
> 
> Thanks,
> 
> Brijesh
> 
>  
> 
> 
> 


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