java.io.IOException:连接上的流意外结束? [英] java.io.IOException: unexpected end of stream on Connection?
问题描述
调用我们的内部Web服务之一似乎出现以下错误:
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)
根据我在其他地方看到的内容,这被认为是服务器问题,但是当在浏览器或IOS中调用WS时,我们没有看到此问题.我同时使用了OkHTTP和手动HTTPUrlConnection实现,但似乎没有任何区别.有人有什么想法吗?
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();
推荐答案
(在我看来)请尝试以下可能的解决方案:
Please try the potential solutions (in my opinion) below:
1-尝试如下使用retryOnConnectionFailure(true)
:
OkHttpClient client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
2-尝试添加:request.addHeader("Connection","close")
(大多数情况下,这是解决方案,尊重Kartik的回答.)
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-请增加服务器的响应时间(将其设置为尽可能高的值),然后重试.
3- Please increase the response time of the server (set it as high as possible) and try again.
另请参阅: OkHTTP Websocket:Connection上的蒸汽意外终止
这篇关于java.io.IOException:连接上的流意外结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!