我收到以下代码的连接重置错误。导致连接重置的问题到底出在哪里?是因为我试图忽略SSL证书验证的方式吗?
String serviceUri = "https://service.providers.com/applications";
String reqJson = "request json string";
//this is to ignore SSL validation.
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(builder.build(),
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslSF).build();;
HttpPost postRequest = new HttpPost(serviceUri);
StringEntity input = new StringEntity(reqJson);
input.setContentType("application/json");
postRequest.setEntity(input);
CloseableHttpResponse response = httpClient.execute(postRequest);
try {
//do some stuff
///
//make sure you consume the entire response
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} catch (IllegalStateException e) {
e.printStackTrace();
}finally{
response.close();
}
堆栈跟踪
java.net.SocketException:连接重置于java.net.SocketInputStream.read(SocketInputStream.java:179)位于com.ibm.jsse2.a.a(a.java:148)位于com.ibm.jsse2.a.a(a.java:96)位于com.ibm.jsse2.tc.g(tc.java:208)上的com.ibm.jsse2.tc.a(tc.java:302)在com.ibm.jsse2.tc.a(tc.java:482)com.ibm.jsse2.tc.startHandshake(tc.java:597)在org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:275)在org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254)在org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:117)在org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:314)在org.apache.http.impl.execchain.MainClientExec。EstablishmentRoute(MainClientExec.java:363)在org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)在org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)在org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)在org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)在org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)