gpt4 book ai didi

java - 是否需要显式关闭 CloseableHttpClient 和 CloseableHttpResponse

转载 作者:行者123 更新时间:2023-11-30 10:00:19 62 4
gpt4 key购买 nike

我有以下方法,它发出一个 HTTP POST 请求。

它返回一个CloseableHttpResponse,因此任何调用它的代码都可以获得状态代码、响应主体等。

我正在尝试理解,是否:

  • CloseableHttpClient 客户端
  • CloseableHttpResponse closeableHttpResponse

需要关闭吗?还是关闭一个,关闭另一个?

..
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 block :

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();
}
}
}
}

我认为更好的答案是使用 try-with-resources statement :

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();
}
}
}

一般来说,我看到人们只为他们的应用程序创建一个 CloseableHttpClient,然后在他们的应用程序中重用该实例。如果您只想一遍又一遍地使用一个或几个实例,那么不,我认为您不必关闭它们。但是,如果您要一遍又一遍地创建 CloseableHttpClient 的新实例,那么是的,您将需要关闭它们。

关于java - 是否需要显式关闭 CloseableHttpClient 和 CloseableHttpResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58070374/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com