开发者

java.io.EOFException: Unexpected end of ZLIB input stream - reading from HTTP

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:

Ht开发者_JAVA技巧tpURLConnection 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!!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜