11

I'm using CloseableHttpResponse (from apache-httpclient-4.5.3) and I'm not sure I'm using it right, I saw an answer with no votes to use EntityUtils.consume on finally:

CloseableHttpResponse response1 = httpclient.execute(httpGet);

try {

 System.out.println(response1.getStatusLine());

} finally {

 EntityUtils.consume(response1.getEntity());

CloseableHttpClient is abstract and has no close method to call although in this answer it's used:

CloseableHttpResponse response = httpclient.execute(httpget);
try {
    //do something
} finally {
    response.close();
}

Currently I'm using try with resources for CloseableHttpClient and CloseableHttpResponse inside of send method.

Am I not missing any resource open or using it in a wrong way?

private CloseableHttpResponse send()
            throws URISyntaxException, UnsupportedEncodingException, IOException, ClientProtocolException {
        URIBuilder uriBuilder = new URIBuilder(BASE_URL);
        HttpHost target = new HttpHost(uriBuilder.getHost(), uriBuilder.getPort(), uriBuilder.getScheme());
        HttpPost post = new HttpPost(uriBuilder.build());
        try (CloseableHttpClient httpClient = HttpClients.custom().build(); CloseableHttpResponse response = httpClient.execute(target, post)) {            
            return response;
        }
    
Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Just a note: CloseableHttpResponse response1 = httpclient.execute(httpGet), the execute method is deprecated on version 5.2.1. – cviniciusm May 18 '23 at 12:33

2 Answers2

17

It has been explained in detail in the docs here.
Quoting the pseudo code from the docs here's a typical way to allocate/deallocate an instance of CloseableHttpClient:

try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
    <...>
}

The same applies to CloseableHttpResponse :

try (CloseableHttpResponse response = httpclient.execute(httpget)) {
    <...>
}

Now, about the close method in CloseableHttpClient. CloseableHttpClient is an abstract class that implements Closeable interface. That is, although it doesn't have a close method itself the classes that extend it are required to implement the close method. One class is InternalHttpClient. You can check the source code for the details.

Before Java7, explicit close would be required:

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
    <...>
} finally {
    httpclient.close();
}

CloseableHttpResponse response = httpclient.execute(httpget);
try {
    <...>
} finally {
    response.close();
}
atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • 1
    My method with `try with resources` is equally acceptable? – Ori Marko Feb 22 '18 at 14:06
  • Yes. There are several ways to deallocated `Closeable` resources, one of which would be try-with-resources. It depends on your use case how and when to close your resources, however. – atoMerz Feb 22 '18 at 14:08
  • In this case I just create request and execute it. it seems right way to use `try with resources` – Ori Marko Feb 22 '18 at 14:11
  • there's no need for `EntityUtils.consume(response.getEntity())`? – Ori Marko Feb 22 '18 at 14:20
  • @user7294900 There is, in case you are not sure whether or not your code always consumes the entire response message content. And try-with-resources is the right way but it does not negate the basic principle outlined by atoMerz – ok2c Feb 22 '18 at 14:38
  • @oleg This is my code, and I can't use `response` in finally if it's inside `try with resources` – Ori Marko Feb 22 '18 at 14:43
  • @user7294900 so what? The response gets closed implicitly by the runtime – ok2c Feb 22 '18 at 14:54
  • And yet they say here that is threadsafe and recommend that one instance be used for multiple requests? https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e213 – slashdottir Oct 13 '20 at 19:26
  • @slashdottir It seems to me this is a new question posted as a comment. I'd recommend posting a new question and explaining in more detail what it is you're looking for. It is not clear to me as of now what you're asking. – atoMerz Oct 15 '20 at 11:21
8

You can avoid the finally by using the try(resource)

try (CloseableHttpResponse response = httpclient.execute(httpGet)) { ... }

dvallejo
  • 1,033
  • 11
  • 25