有同样的问题。 其他答案似乎没有解决为什么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.
另外,关系如下:
client.close()(接口)
实施者:
client.close()-线程安全
client.close()-不推荐使用ThreadSafe BUT,请改用client。
client.close()-不是ThreadSafe,但会创建ThreadSafe client。
用于创建CUSTOM client.close()。
client.close()-不是ThreadSafe,但会创建ThreadSafe client。
用于创建默认或最小client.close()。
根据Apache的首选方式:
CloseableHttpClient httpclient = HttpClients.createDefault();
他们给出的示例在client子句中执行client.close(),并且还利用了CloseableHttpClient。
另外,mkyong的执行方式也很有趣:
HttpClient client = HttpClientBuilder.create().build();
他没有显示client.close()电话,但我认为这是必要的,因为client仍然是CloseableHttpClient的一个实例。