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

自作多情 提交于 2019-12-08 21:44:22

问题


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();

回答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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/52726909/java-io-ioexception-unexpected-end-of-stream-on-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!