java - java.io.IOException:连接上的流意外结束?
问题描述
调用我们的内部 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)
从我在其他地方看到的情况来看,这被认为是服务器问题,但是当在浏览器或 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();
解决方案
请尝试以下潜在的解决方案(在我看来):
1-尝试retryOnConnectionFailure(true)
如下使用:
OkHttpClient client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
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-请增加服务器的响应时间(设置尽可能高),然后重试。
推荐阅读
- javascript - jquery-无法从我通过ajax调用获得的对象类型响应中检索数据
- python - 如何打包依赖于 tflite_runtime 的 python 项目?
- c# - 如何让实例化对象在实例化后继续跟随我的玩家?
- javascript - 无法从静态方法访问 Javascript 对象中的功能属性
- node.js - Node.js + Powershell:使用 stdin.write 时如何编码 Unicode 字符
- python - 奇怪:调用python方法使其运行两次
- java - Spring MVC 休息控制器 404
- jquery - 无法触发点击黄金时段日历
- python - 返回每列或每行表现最低的 df
- splunk-query - 使用正则表达式模式忽略结果的 Splunk 查询