0

Hi I am trying to connect to a service , but it fails with error below on calling

 CloseableHttpResponse response = httpclient.execute(httpget);
    data = IOUtils.toString(response.getEntity().getContent(),
                    StandardCharsets.UTF_8);

I am not calling close on response. Hence, I am assuming this could not be because the connection was closed before the entire data was read? How can I handle this in my code? How should this be handled in production ? Is this an issue with my code or the service?

Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:221) ~[?:1.8.0]
    at java.net.SocketInputStream.read(SocketInputStream.java:152) ~[?:1.8.0]
    at com.ibm.jsse2.b.a(b.java:240) ~[?:8.0 build_20171107]
    at com.ibm.jsse2.b.a(b.java:142) ~[?:8.0 build_20171107]
    at com.ibm.jsse2.at.a(at.java:630) ~[?:8.0 build_20171107]
    at com.ibm.jsse2.at.a(at.java:893) ~[?:8.0 build_20171107]
    at com.ibm.jsse2.f.read(f.java:60) ~[?:8.0 build_20171107]
    at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:139) ~[httpcore-4.4.4.jar:4.4.4]
    at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:155) ~[httpcore-4.4.4.jar:4.4.4]
    at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:284) ~[httpcore-4.4.4.jar:4.4.4]
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261) ~[httpcore-4.4.4.jar:4.4.4]
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165) ~[httpcore-4.4.4.jar:4.4.4]
    at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272) ~[httpcore-4.4.4.jar:4.4.4]
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124) ~[httpcore-4.4.4.jar:4.4.4]
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.5.2.jar:4.5.2]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) ~[httpclient-4.5.2.jar:4.5.2]
tech_questions
  • 223
  • 3
  • 13
  • Possible duplicate of [java.net.SocketException: Connection reset](https://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset) – Gary May 29 '18 at 13:59

1 Answers1

0

It seems like the entity content can't be streamed because it is closed. Connection reset usually tells you that there's something wrong with your GET request/response.

However, you may also want to look into this:

What's the recommended way to get the HTTP response as a String when using Apache's HTTP Client?

The accepted answer shows a good way to handle HTTP responses (using EntityUtils and HttpResponse).

Konrad Kar
  • 11
  • 4