java Apache HttpClient 的正确使用以及何时关闭它。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43454514/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:26:08  来源:igfitidea点击:

Proper usage of Apache HttpClient and when to close it.

javaservletshttpclient

提问by Half_Duplex

I am using HttpClient within a servlet to make calls to a resource which I return as the servlets response after some manipulation.

我在 servlet 中使用 HttpClient 来调用资源,在一些操作后我将其作为 servlet 响应返回。

My HttpClient uses PoolingHttpClientConnectionManager.

我的 HttpClient 使用 PoolingHttpClientConnectionManager。

I create the client like so:

我像这样创建客户端:

private CloseableHttpClient getConfiguredHttpClient(){
    return HttpClientBuilder
        .create()
        .setDefaultRequestConfig(config)
        .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
        .setConnectionManagerShared(true)
        .setConnectionManager(connManager)
        .build();
}

I use this client within a Try With Resource within the servlets service method, so it is auto closed. To stop the the connection manager from being closed, I set setConnectionManagerSharedto true.

我在 servlets 服务方法中的 Try With Resource 中使用此客户端,因此它会自动关闭。为了阻止连接管理器被关闭,我设置setConnectionManagerShared为 true。

I have seen other code samples that do not close the HttpClient. Should I not be closing this resource?

我见过其他不关闭 HttpClient 的代码示例。我不应该关闭这个资源吗?

Thanks

谢谢

采纳答案by Matt

For other versions of httpcomponents, see other answers.

对于其他版本的 httpcomponents,请参阅其他答案。

For older versions of httpcomponents (http://hc.apache.org/httpcomponents-client-4.2.x/quickstart.html):

对于旧版本的 httpcomponents ( http://hc.apache.org/httpcomponents-client-4.2.x/quickstart.html):

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution.

您不需要显式关闭 HttpClient,但是(您可能已经这样做了但值得注意)您应该确保在方法执行后释放连接。

Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.

编辑: HttpClient 中的 ClientConnectionManager 将负责维护连接状态。

 GetMethod httpget = new GetMethod("http://www.url.com/");
  try {
    httpclient.executeMethod(httpget);
    Reader reader = new InputStreamReader(httpget.getResponseBodyAsStream(), httpget.getResponseCharSet()); 
    // consume the response entity and do something awesome
  } finally {
    httpget.releaseConnection();
  } 

回答by Kirill Ch

For httpcomponents version 4.5.x:

对于 httpcomponents 版本 4.5.x:

I found that you really need to close the resource as shown in the documentation: https://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html

我发现您确实需要按照文档中所示关闭资源:https: //hc.apache.org/httpcomponents-client-4.5.x/quickstart.html

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();
    EntityUtils.consume(entity1);
} finally {
    response1.close();
}