java 关闭 CloseableHttpResponse/CloseableHttpClient 的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48928930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Right way to close CloseableHttpResponse/CloseableHttpClient
提问by user7294900
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 votesto use EntityUtils.consume
on finally:
我使用CloseableHttpResponse(来自Apache的HttpClient的-4.5.3),我不知道我使用的是正确的,我看到了一个不带票的答案用EntityUtils.consume
最后在:
CloseableHttpResponse response1 = httpclient.execute(httpGet); try { System.out.println(response1.getStatusLine()); } finally { EntityUtils.consume(response1.getEntity());
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 answerit's used:
CloseableHttpClient
是抽象的,没有 close 方法可以调用,尽管在这个答案中使用了它:
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.
目前我正在使用 tryCloseableHttpClient
和CloseableHttpResponse
send 方法内部的资源。
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;
}
采纳答案by atoMerz
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
:
它已在此处的文档中详细解释。
从这里引用文档中的伪代码是分配/取消分配实例的典型方法CloseableHttpClient
:
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
<...>
}
The same applies to CloseableHttpResponse
:
这同样适用于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.
现在,关于CloseableHttpClient
. CloseableHttpClient
是一个实现Closeable
接口的抽象类。也就是说,虽然它本身没有close
方法,但需要扩展它的类来实现该close
方法。一类是InternalHttpClient
。您可以查看源代码以了解详细信息。
Before Java7, explicit close would be required:
在 Java7 之前,需要显式关闭:
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
<...>
} finally {
httpclient.close();
}
CloseableHttpResponse response = httpclient.execute(httpget);
try {
<...>
} finally {
response.close();
}
回答by dvallejo
You can avoid the finally by using the try(resource)
您可以通过使用 try(resource) 来避免 finally
try (CloseableHttpResponse response = httpclient.execute(httpGet)) { ... }
尝试 (CloseableHttpResponse 响应 = httpclient.execute(httpGet)) { ... }