java.io.EOFException:ZLIB 输入流意外结束 - 从 HTTP 读取
我尝试四处寻找类似的问题,但找不到任何与我的问题类似的解决方案:
我使用以下代码从 HttpUrlConnection 读取:
public static BufferedReader getConnectionReader(HttpURLConnection con, String url)
throws Exception {
con = (HttpURLConnection) new URL(url).openConnection();
con.connect();
if (cm != null) {
cm.storeCookies(con);
}
if (con.getHeaderField("Content-Encoding") != null
&& con.getHeaderField("Content-Encoding").equalsIgnoreCase("gzip")) {
return new BufferedReader(new InputStreamReader(new GZIPInputStream(con.getInputStream())));
} else
return new BufferedReader(new InputStreamReader(con.getInputStream()));
}
读取按以下方式执行:
HttpURLConnection con = null;
reader = Utils.getConnectionReader(con, "http://www.site.com/page.html");
String line = null;
while ((line = reader.readLine()) != null) {
log.info(line);
}
有时我会收到提到的异常:
java.io.EOFException:ZLIB 输入流意外结束
当我可以时,我捕获此异常并重试操作 - 成功。
问题是我不知道是什么导致了这个异常的发生。 它发生得很随机。
我愿意相信这是一个网络问题。
有人找到完全解决此类问题的方法吗?
谢谢!!
I tried looking around for similar problem but couldn't find any solution that resemble my problem:
I use the following piece of code to read from HttpUrlConnection:
public static BufferedReader getConnectionReader(HttpURLConnection con, String url)
throws Exception {
con = (HttpURLConnection) new URL(url).openConnection();
con.connect();
if (cm != null) {
cm.storeCookies(con);
}
if (con.getHeaderField("Content-Encoding") != null
&& con.getHeaderField("Content-Encoding").equalsIgnoreCase("gzip")) {
return new BufferedReader(new InputStreamReader(new GZIPInputStream(con.getInputStream())));
} else
return new BufferedReader(new InputStreamReader(con.getInputStream()));
}
Reading is performed in the following way:
HttpURLConnection con = null;
reader = Utils.getConnectionReader(con, "http://www.site.com/page.html");
String line = null;
while ((line = reader.readLine()) != null) {
log.info(line);
}
Sometimes I get the mentioned exception:
java.io.EOFException: Unexpected end of ZLIB input stream
When I can, I catch this exception and retry the operation - successfully.
The problem is that I don't know what is causing this exception to pop.
It happens quite randomly.
I want to believe it is a network issue.
Anyone found a way to fully solve such problem?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的方法对于像 GZip 这样的二进制格式来说不太理想,我假设您这样做只是为了测试?除了这个问题和 HTTPURLConnection 的错误之外,代码中没有太多其他内容可能导致该问题。我建议使用字节缓冲区交换进行读取,至少可以消除这种可能的错误来源。
The method your using is less than ideal for binary formats like GZip, I assume you are doing that just for testing? Apart from that and the bug with the HTTPURLConnection, there's not much else in code that could be causing the issue. I would recommend reading using byte buffer swapping, at least to eliminate that as a possible source of error.