Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between CloseableHttpResponse.close() and httpPost.releaseConnection()?

Tags:

CloseableHttpResponse response = null; try {     // do some thing ....     HttpPost request = new HttpPost("some url");     response = getHttpClient().execute(request);     // do some other thing .... } catch(Exception e) {     // deal with exception } finally {     if(response != null) {         try {             response.close(); // (1)         } catch(Exception e) {}         request.releaseConnection(); // (2)     } } 

I've made a http request like above.

In order to release the underlying connection, is it correct to call (1) and (2)? and what's the difference between the two invocation?

like image 214
wyang Avatar asked Jun 17 '15 11:06

wyang


People also ask

What is difference between CloseableHttpClient and HttpClient?

CloseableHttpClient is the base class of the httpclient library, the one all implementations use. Other subclasses are for the most part deprecated. The HttpClient is an interface for this class and other classes. You should then use the CloseableHttpClient in your code, and create it using the HttpClientBuilder .

What is HttpClients createDefault ()?

createDefault() Creates CloseableHttpClient instance with default configuration. static CloseableHttpClient. createMinimal() Creates CloseableHttpClient instance that implements the most basic HTTP protocol support.

How do I close CloseableHttpClient?

Start a try-finally block, write the remaining code in the programs in the try block and close the CloseableHttpClient object in the finally block.


1 Answers

Short answer:

request.releaseConnection() is releasing the underlying HTTP connection to allow it to be reused. response.close() is closing a stream (not a connection), this stream is the response content we are streaming from the network socket.

Long Answer:

The correct pattern to follow in any recent version > 4.2 and probably even before that, is not to use releaseConnection.

request.releaseConnection() releases the underlying httpConnection so the request can be reused, however the Java doc says:

A convenience method to simplify migration from HttpClient 3.1 API...

Instead of releasing the connection, we ensure the response content is fully consumed which in turn ensures the connection is released and ready for reuse. A short example is shown below:

CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://targethost/homepage"); CloseableHttpResponse response1 = httpclient.execute(httpGet); try {     System.out.println(response1.getStatusLine());     HttpEntity entity1 = response1.getEntity();     // do something useful with the response body     String bodyAsString = EntityUtils.toString(exportResponse.getEntity());     System.out.println(bodyAsString);     // and ensure it is fully consumed (this is how stream is released.     EntityUtils.consume(entity1); } finally {     response1.close(); } 
like image 129
Matt Avatar answered Oct 12 '22 07:10

Matt