java.io.IOException:连接上的流意外结束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52726909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:36:00  来源:igfitidea点击:

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

javaandroidhttpurlconnectionokhttp

提问by Benjamin S

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

调用我们的内部 Web 服务之一似乎会出现以下错误:

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?

从我在其他地方看到的情况来看,这被认为是服务器问题,但是当在浏览器或 IOS 中调用 WS 时,我们没有看到这个问题。我使用了 OkHTTP 和手动 HTTPUrlConnection 实现,它似乎没有任何区别。有没有人有任何想法?

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

回答by aanshu

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

此错误TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384与服务器有关。一个可能的原因可能是,服务器已关闭。

回答by Kartik

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").

在您的OkHttp要求建设者,加入这一行:.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.

还要检查浏览器检查器的网络选项卡以查看浏览器发送的标头。匹配代码中的这些标头,它应该可以工作。

回答by Eray Balkanli

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

请尝试以下潜在的解决方案(在我看来):

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

1-尝试使用retryOnConnectionFailure(true)如下:

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.)

2- 尝试添加:(request.addHeader("Connection","close")大多数情况下这是解决方案,关于 Kartik 的回答。)

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.

3- 请增加服务器的响应时间(设置尽可能高),然后重试。

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

另请参阅:OkHTTP Websocket:连接上的蒸汽意外结束