Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I/O error on POST request for... java.net.SocketException: Connection reset

I am using Spring Rest Template to call a REST service that is hosted at an SSL endpoint. After the first request, I am getting the below error. The environment is AWS EC2, Open JDK 1.8.161, Linux. The endpoint only supports TLSv1.2 and 1.3.

 org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://mycompany.com": Connection reset; nested exception is java.net.SocketException: Connection reset
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743)
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:686)
        at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:437)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    ... more stack here
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
    at .....
    ... 107 common frames omitted

Any guidance will be greatly appreciated=!!

like image 739
user2402933 Avatar asked Oct 16 '18 13:10

user2402933


People also ask

What causes a Java net SocketException connection reset?

java.net.SocketException: Connection reset This SocketException occurs on the server-side when the client closed the socket connection before the response could be returned over the socket. For example, by quitting the browser before the response was retrieved. Connection reset simply means that a TCP RST was received.

How do I fix Java net SocketException connection reset on realms?

Close Minecraft: Java Edition and reopen it. Restart your computer or device and see if this fixes the issue. Restarting can often clear errors in connections. Log out of your Microsoft or Mojang account, closing the game, and then log in again.


1 Answers

This quite feels like the protocol issue. The AWS is fully TLS 1.2 or higher supporting, but the client is not. The Rest Template if not configured otherwise, will start negotiation with TLS 1.0. You have to forcefully make the client to connect only with 1.2 or 1.3 protocol stacks.

In general, this can be configured while creating the client. Something similar to this

SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null, null, null);

CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(context)
    .build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);

You can find how to tune the template based on the spring version. Forcing the client to negotiate only on the allowed protocols will solve this problem.

like image 83
Kris Avatar answered Oct 09 '22 23:10

Kris