Java 连接重置错误-REST服务客户端
我收到连接重置错误,代码如下。到底是什么原因导致了连接重置?这是因为我试图忽略SSL证书验证的方式吗Java 连接重置错误-REST服务客户端,java,ssl,apache-commons-httpclient,Java,Ssl,Apache Commons Httpclient,我收到连接重置错误,代码如下。到底是什么原因导致了连接重置?这是因为我试图忽略SSL证书验证的方式吗 String serviceUri = "https://service.providers.com/applications"; String reqJson = "request json string"; //this is to ignore SSL validation. SSLContextBuilder builder = new SSLContextBuilder(); bui
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.a(tc.java:302)位于com.ibm.jsse2.tc.g(tc.java:208)
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.poolighttpclientconnectionmanager.connect(poolighttpclientconnectionmanager.java:314)
在
org.apache.http.impl.execchain.MainClientExec.buildRoute(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)
我能够解决这个问题。这原来是一个与网络有关的问题。从安装在本地计算机上的服务器发出的请求未通过代理服务器路由。但是我们公司的防火墙不喜欢发出跳过代理的请求,所以它正在删除连接。一旦我将代理路由规划器添加到http请求中,我的请求就会通过