Java CloseableHttpClient和CloseableHttpResponse是否都需要显式关闭
我有以下方法,它发出Java CloseableHttpClient和CloseableHttpResponse是否都需要显式关闭,java,Java,我有以下方法,它发出HTTPPOST请求 它返回一个CloseableHttpResponse,因此任何调用它的代码都可以获得状态代码、响应正文等 我试图理解,这两种情况: CloseableHttpClient客户端 CloseableHttpResponse可关闭httpresponse 需要关门吗?还是关闭一个,关闭另一个 .. import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.
HTTP
POST
请求
它返回一个CloseableHttpResponse
,因此任何调用它的代码都可以获得状态代码、响应正文等
我试图理解,这两种情况:
CloseableHttpClient客户端
CloseableHttpResponse可关闭httpresponse
..
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
public static CloseableHttpResponse post(final String restEndpoint, final String data, final Header[] headers) throws Exception {
final URIBuilder uriBuilder = new URIBuilder(restEndpoint);
final HttpPost httpPost = new HttpPost(uriBuilder.build());
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Cache-Control", "no-cache, no-store");
if (data != null) {
httpPost.setEntity(new StringEntity(data));
}
if (headers != null) {
for (Header header : headers) {
httpPost.setHeader(header);
}
}
final CloseableHttpClient client = HttpClients.custom()
.setSSLSocketFactory(createSSLFactory())
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
final CloseableHttpResponse closeableHttpResponse = client.execute(httpPost);
final int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
logger.debug(Optional.empty(), statusCode, httpPost.toString());
return closeableHttpResponse;
}
我认为每个请求都需要手动关闭CloseableHttpResponse
有几种方法可以做到这一点
使用try/catch/finally块:
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
public class CloseableHttpClientWithTryCatchFinally {
public static void main(String... args) throws Exception {
URIBuilder uriBuilder = new URIBuilder("https://www.google.com/");
HttpGet httpGet = new HttpGet(uriBuilder.build());
CloseableHttpClient client = HttpClients.custom().build();
CloseableHttpResponse response = null;
try {
response = client.execute(httpGet);
response.getEntity().writeTo(System.out);
} catch (IOException e) {
System.out.println("Exception: " + e);
e.printStackTrace();
} finally {
if (response != null) {
response.close();
}
}
}
}
我认为更好的答案是使用:
一般来说,我看到人们只是为他们的应用程序创建一个CloseableHttpClient
,然后在整个应用程序中重用该实例。如果您只打算反复使用一个或几个实例,那么不,我认为您不应该关闭它们。但是,如果要反复创建CloseableHttpClient
的新实例,则需要关闭它们
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
public class CloseableHttpClientTryWithResources {
public static void main(String... args) throws Exception {
URIBuilder uriBuilder = new URIBuilder("https://www.google.com/");
HttpGet httpGet = new HttpGet(uriBuilder.build());
CloseableHttpClient client = HttpClients.custom().build();
try (CloseableHttpResponse response = client.execute(httpGet)) {
response.getEntity().writeTo(System.out);
} catch (IOException e) {
System.out.println("Exception: " + e);
e.printStackTrace();
}
}
}