测试版本 httpclient 4.5.13
public static String doGetRequest(String url) throws IOException {
HttpGet httpGet = new HttpGet(url.trim());
CloseableHttpClient client = getClient();
String body = client.execute(httpGet, new BasicResponseHandler());
return body;
}
org.apache.http.util.EntityUtils.toString(entity, ContentType.get(entity))
inStream.close();
org.apache.http.impl.client.CloseableHttpClient.execute(httpGet, new BasicResponseHandler())
response.close();
org.apache.http.impl.client.InternalHttpClient.close
CloseableHttpClient.close() 关闭所有资源
如果使用了 PoolingHttpClientConnectionManager 并且配置了evictIdleConnections 则会:
-
IdleConnectionEvictor 中断evictor线程
-
HttpClientConnectionManager.shutdown() 关闭连接 连接池
小结
以上doGetRequest中HttpClient会自行关闭inStream与response
HttpClient使用PoolingHttpClientConnectionManager时存在以下资源:
- IdleConnectionEvictor Thread
- PoolingHttpClientConnectionManager 连接池与连接
正常情况下不需要关闭HttpClient,Evictor Thread会自动关闭空闲连接。
如果不在使用或服务停止,可以调用HttpClient.close(),释放所有资源。