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

3

14

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();
Manny answered 9/10, 2018 at 18:3 Comment(3)
Are you using some sort of proxy?Feticide
Do remote server expect 2 way ssl (requests client certificate)?Gentlemanfarmer
As far as I'm able to tell this may be a problem only applicable to my Android emulation. When I switched off of emulation and onto a physical device I appear to not be having issues with this error occurring any longer. I still don't understand why this would stop working when previously I was able to use the emulator fine. Potentially an issue with an update to the emulator at some point?Manny
R
12

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

Renny answered 6/11, 2018 at 19:57 Comment(0)
M
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.

Mg answered 2/11, 2018 at 0:25 Comment(0)
O
0

This error TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 is related to server. A possible reason could be, the server is shut down.

Oralle answered 31/10, 2018 at 18:28 Comment(3)
Appreciate the response, but no, I'm able to consistently call the services using other devices.Manny
is it a device speci9?Oralle
This is not an error, it is a TLS cipher suite.Selassie

© 2022 - 2024 — McMap. All rights reserved.