有同样的问题。其他答案似乎没有解决为什么close()真的有必要?此外,Op似乎正在努力找出使用HttpClient等人的首选方式。
根据Apache的说法:
// The underlying HTTP connection is still held by the response object
// to allow the response content to be streamed directly from the network socket.
// In order to ensure correct deallocation of system resources
// the user MUST call CloseableHttpResponse#close() from a finally clause.
此外,这些关系如下所示:
HttpClient(接口)
实施者:
CloseableHttpClient- 线程安全。
DefaultHttpClient- 线程安全但已弃用,请改用。HttpClientBuilder
HttpClientBuilder- 不是线程安全,而是创建线程安全。CloseableHttpClient
- 用于创建自定义 。
CloseableHttpClient
HttpClients- 不是线程安全,而是创建线程安全。CloseableHttpClient
- 用于创建默认或最小 。
CloseableHttpClient
根据Apache的首选方式:
CloseableHttpClient httpclient = HttpClients.createDefault();
他们给出的例子在子句中确实如此,并且也使用了。httpclient.close()finallyResponseHandler
作为替代方案,mkyong这样做的方式也有点有趣:
HttpClient client = HttpClientBuilder.create().build();
他没有显示呼叫,但我认为这是必要的,因为仍然是.client.close()clientCloseableHttpClient