gpt4 book ai didi

java - 如何解决org.apache.http.NoHttpResponseException

转载 作者:行者123 更新时间:2023-12-03 22:11:13 28 4
gpt4 key购买 nike

我正在使用 apache httpclient 4.4 来做 https 请求,我可以不断地看到 org.apache.http.NoHttpResponseException .

这是我创建 httpclient 的代码。

   TrustManager[] trustAllCerts =
new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

@Override
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
}

} };
SSLContext sslcontext = null;
try {
sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
logger.warn(e.getMessage());
} catch (KeyManagementException e) {
logger.warn(e.getMessage());
}
// Allow TLSv1 protocol only
final SSLConnectionSocketFactory sslsf =
new SSLConnectionSocketFactory(sslcontext,
new String[] { "TLSv1" }, null, NoopHostnameVerifier.INSTANCE);

Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create().register("https", sslsf)
.build();
PoolingHttpClientConnectionManager cm =
new PoolingHttpClientConnectionManager(socketFactoryRegistry);

// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);

在执行请求之前,我做 cm.closeExpiredConnections();conMgr.closeIdleConnections(30, TimeUnit.SECONDS) .我在阅读 this question 的答案后添加了这个

还有什么我需要做的吗?

最佳答案

解决此问题的另一种方法是配置您的 org.apache.http.impl.client.AbstractHttpClient httpClient永远不要重用连接:

httpClient.setReuseStrategy(new NoConnectionReuseStrategy());

同样可以在 org.apache.http.impl.client.HttpClientBuilder builder 上配置:
builder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());

关于java - 如何解决org.apache.http.NoHttpResponseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29006222/

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