0
votes

I'm getting Connection Reset error with the following code. What exactly is wrong with it that's causing connection resets? Is it because of the way I'm trying to ignore SSL cert validation?

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();
}

Stack trace

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)

1

1 Answers

0
votes

I was able to figure out the issue. It turned out be a network related issue. The requests made from the server installed on my local machine are not getting routed through the proxy server. But our company's firewall doesn't like out going requests to skip the proxy, so it is dropping the connection. Once I added the proxy route planner to the http request, my requests are going through.