<分区>
我正在使用 CloseableHttpResponse(来自 apache-httpclient-4.5.3)但我不确定我是否正确使用它,我看到了一个 answer with no votes最后使用 EntityUtils.consume
:
CloseableHttpResponse response1 = httpclient.execute(httpGet); try { System.out.println(response1.getStatusLine()); } finally { EntityUtils.consume(response1.getEntity());
CloseableHttpClient
是抽象的,虽然在 answer 中没有调用 close 方法。它被使用:
CloseableHttpResponse response = httpclient.execute(httpget);
try {
//do something
} finally {
response.close();
}
目前,我在发送方法中尝试使用 CloseableHttpClient
和 CloseableHttpResponse
的资源。
我是否遗漏了任何打开的资源或以错误的方式使用它?
private CloseableHttpResponse send()
throws URISyntaxException, UnsupportedEncodingException, IOException, ClientProtocolException {
URIBuilder uriBuilder = new URIBuilder(BASE_URL);
HttpHost target = new HttpHost(uriBuilder.getHost(), uriBuilder.getPort(), uriBuilder.getScheme());
HttpPost post = new HttpPost(uriBuilder.build());
try (CloseableHttpClient httpClient = HttpClients.custom().build(); CloseableHttpResponse response = httpClient.execute(target, post)) {
return response;
}