6

查看快速入门指南,它提供了以下代码示例:

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
// 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.
// Please note that if response content is not fully consumed the underlying
// connection cannot be safely re-used and will be shut down and discarded
// by the connection manager. 
try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity1);
} finally {
    response1.close();
}

上面代码中的两条注释说我们必须关闭响应对象

“正确释放系统资源”

“如果响应内容未完全消耗,则无法安全地重新使用底层连接,并将被连接管理器关闭并丢弃”。

现在 Apache 非常友好地为我们实现了 CloseableHttpResponse,这意味着我们可以使用 try-with-resources 块。但是close方法只是关闭了响应对象,为什么不消费实体呢?

4

1 回答 1

3

因为此时很难说调用者是否打算重用底层连接。在某些情况下,可能只想从大型响应正文中读取一小块并立即终止连接。

换句话说,同样的事情一遍又一遍地发生:没有一种方法可以让每个人都开心。

代码片段将确保在尝试保持底层连接活动的同时正确取消资源分配。

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
    System.out.println(response1.getStatusLine());
} finally {
    EntityUtils.consume(response1.getEntity());
} 
于 2017-06-10T11:16:14.310 回答