java.io.IOException: unexpected end of stream on Connection?

33,001

Solution 1

Please try the potential solutions (in my opinion) below:

1- Try to use retryOnConnectionFailure(true) as below:

OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();

2- Try to add: request.addHeader("Connection","close") (most cases this is the solution, respecting to Kartik's answer.)

Request request = new Request.Builder()
                    .addHeader("Authorization", "Bearer " + apiToken)
                    .addHeader("content-type", "application/json")
                    .addHeader("Connection","close")
                    .url(uri)
                    .build();

3- Please increase the response time of the server (set it as high as possible) and try again.

Also see: OkHTTP Websocket: Unexpected end of steam on Connection

Solution 2

Both the client and server needs to close the connection once they are finished.

In your OkHttp request builder, add this line: .header("Connection", "close").

Also check the network tab of your browser inspector to see which headers the browser is sending. Match those headers in your code and it should work.

Share:
33,001
Benjamin S
Author by

Benjamin S

Software Developer working in Java, SQL and PL/B. One of these languages is not like the other...

Updated on August 27, 2020

Comments

  • Benjamin S
    Benjamin S over 3 years

    Calling one of our in-house web services seems to be giving the following error:

    java.io.IOException: unexpected end of stream on Connection{webservicessandbox.xxx.com:443, proxy=DIRECT@ hostAddress=174.143.185.13 cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA protocol=http/1.1} (recycle count=0)
    

    From what I've seen elsewhere, this is being pointed to as a server issue, but we're not seeing this problem when the WS is being called in browser or in IOS. I've used both OkHTTP and a manual HTTPUrlConnection implementation and it hasn't seemed to make any difference. Does anyone have any ideas?

    OKHttp:
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .addHeader("Authorization", "Bearer " + apiToken)
                        .addHeader("content-type", "application/json")
                        .url(uri)
                        .build();
    HTTPURLConnection:
                URL myURL = new URL(uri);
                HttpsURLConnection myConnection = (HttpsURLConnection) myURL.openConnection();
                myConnection.setConnectTimeout(TIMEOUT_MILLISEC);
                myConnection.setDoOutput(false);
                myConnection.setRequestMethod("GET");
                myConnection.setRequestProperty("Authorization", "Bearer " + apiToken);
                myConnection.setRequestProperty("content-type", "application/json");
                int respCode = myConnection.getResponseCode();
    
  • Benjamin S
    Benjamin S over 5 years
    Appreciate the response, but no, I'm able to consistently call the services using other devices.
  • aanshu
    aanshu over 5 years
    is it a device speci9?
  • user207421
    user207421 over 4 years
    This is not an error, it is a TLS cipher suite.