java.io.EOFException:ZLIB 输入流意外结束 - 从 HTTP 读取

发布于 2024-11-25 18:10:45 字数 1153 浏览 0 评论 0原文

我尝试四处寻找类似的问题,但找不到任何与我的问题类似的解决方案:

我使用以下代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2024-12-02 18:10:45

您使用的方法对于像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文