java - 连接重置错误 - REST 服务客户端

标签 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();
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: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:179) at com.ibm.jsse2.a.a(a.java:148) at com.ibm.jsse2.a.a(a.java:96) at com.ibm.jsse2.tc.a(tc.java:302) at com.ibm.jsse2.tc.g(tc.java:208) at com.ibm.jsse2.tc.a(tc.java:482) at com.ibm.jsse2.tc.startHandshake(tc.java:597) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:275) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254) at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:117) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:314) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)

最佳答案

我能够弄清楚这个问题。原来是网络相关的问题。从我本地机器上安装的服务器发出的请求没有通过代理服务器进行路由。但是我们公司的防火墙不喜欢发出跳过代理的请求,所以它正在断开连接。一旦我将代理路由规划器添加到 http 请求,我的请求就会通过。

关于java - 连接重置错误 - REST 服务客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34731726/

相关文章:

android - DefaultHttpClient 到 AndroidHttpClient

android - 在 Android 上使用 EWS Java API 的问题

java - 使用 http 请求通信 Java-Javascript

java - 如何对 HashMap freemarker 模板的值进行排序

python - gevent 和 SSL 导致 "EOF occurred in violation of protocol"

.htaccess - htaccess 从 https 中排除 sitemap.xml 和 robots.txt

ruby - SOAP + SSL + Ruby Savon - HTTPClient::ConnectTimeoutError:执行已过期

java - 如何使用 apache httpClient 设置基本身份验证

java - 在哪里可以找到动手实践的示例 Java 问题?不是认证类型的问题

java - Lombok 注释在带有 maven 的 netbeans 中不起作用