Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 372
Benjamin S Avatar asked Oct 09 '18 18:10

Benjamin S


1 Answers

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

like image 100
Eray Balkanli Avatar answered Sep 21 '22 13:09

Eray Balkanli