以下是我的配置
Java版本" 1.8.0_101"Java(TM)SE运行时环境(构建1.8.0_101-B13)Java热点(TM)64位服务器VM(构建25.101-B13,混合模式)
使用Apache HttpClient v4.4来调用RESTFUL服务,服务URL具有有效证书(SHA2)
使用apache httpclient,我们正在调用该服务。以下是代码
private static CloseableHttpClient appHttpClient() throws Exception {
if (null != httpclient) {
return httpclient;
}
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(trustAllSSLContext())).build();
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setMaxTotal(HTTP_MAX_CON);
cm.setDefaultMaxPerRoute(HTTP_CON_ROUTE);
final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(HTTP_CON_TO)
.setConnectionRequestTimeout(HTTP_REQ_CON_TO).setSocketTimeout(HTTP_SO_TO).build();
// final SSLConnectionSocketFactory factory = new
// SSLConnectionSocketFactory(getSSLContext());
httpclient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build();
return httpclient;
}
public static SSLContext trustAllSSLContext() throws Exception {
if (sslContext != null) {
return sslContext;
}
sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
System.out.println(
"SSLContext :: Protocol :: " + sslContext.getProtocol() + " Provider :: " + sslContext.getProvider());
return sslContext;
}
服务呼叫断断续续地失败了,有批处理流程以循环为单调的服务,例如30个呼叫,因此其中一些人正在失败,其中一些是成功的,因为失败的呼叫,我们的呼叫却低于异常。<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
java.lang.IllegalStateException: SSLContextImpl is not initialized
at sun.security.ssl.SSLContextImpl.engineGetSocketFactory(SSLContextImpl.java:181)
at javax.net.ssl.SSLContext.getSocketFactory(SSLContext.java:294)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>(SSLConnectionSocketFactory.java:262)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>(SSLConnectionSocketFactory.java:205)
at com.wdpr.payment.helper.HttpHelper.appHttpClient(HttpHelper.java:63)
at com.wdpr.payment.helper.HttpHelper.makePostCall(HttpHelper.java:142)...
任何人都知道为什么会间歇性地发生这种例外。
在我在线找到的代码段中,
sslContext = SSLContext.getInstance("");
用" SSL"而不是" TLSV1.2"。
迫使SSL插座使用TLSV1.2,我使用了如何强制CONSONS HTTPCLIENT 3.1仅对HTTPS使用TLS 1.2?
(编辑)
在可能的相关说明中,使用HTTP Commons MultineReadHttpConnectionManager时,我们必须调用
manager.closeIdleConnections(10000);
manager.deleteClosedConnections();
在每个HTTP调用后,否则我们会尝试一些连接失败。
如果'服务URL具有有效证书'是正确的 - 只需删除所有与SSL相关的自定义。
这个(相信他们都是方法):
new SSLConnectionSocketFactory(trustAllSSLContext())
实际上是非valid证书的黑客,作为快速修复 - 尝试将' synchronized
'添加到 trustAllSSLContext
方法。