I've been searching for a completed example of CloseableHttpClient
with try-with-resources. I am confused on if closing CloseableHttpClient
also closes the CloseableHttpResponse
object that will be created when I call httpclient.execute(post)
. Do I need to wrap CloseableHttpResponse
in a try-with-resources too?
Example:
try(CloseableHttpClient httpclient = HttpClients.custom().build()) {
HttpPost post = new HttpPost(url);
CloseableHttpResponse res = httpclient.execute(post);
// do something with res
} catch (Throwable e) {
// do something with error
}