java - EOFException getResponseCode() 异常

标签 java android exception eof

<分区>

我有这段代码:

public static boolean checkIfURLExists(String targetUrl) {
        HttpURLConnection httpUrlConn;
        System.setProperty("http.keepAlive", "false");
        try {
            httpUrlConn = (HttpURLConnection) new URL(targetUrl)
                    .openConnection();
            httpUrlConn.setRequestMethod("HEAD");

            // Set timeouts in milliseconds
            httpUrlConn.setConnectTimeout(500);
            httpUrlConn.setReadTimeout(1000);

            // Print HTTP status code/message for your information.
            return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Removing the URL: " + targetUrl);
            return false;
        }
    }

wich 测试 URL 是否可达。我用不同的 URL 多次调用这段代码。但是,代码在这一行中给出了 EOFException:

return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);

异常(exception):

12-18 11:11:57.655: W/System.err(30198): java.io.EOFException
12-18 11:11:57.655: W/System.err(30198):    at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:206)
12-18 11:11:57.655: W/System.err(30198):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98)
12-18 11:11:57.665: W/System.err(30198):    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:528)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:836)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
12-18 11:11:57.665: W/System.err(30198):    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
12-18 11:11:57.665: W/System.err(30198):    at com.cofely.gdfsuez.XmlDataParseHelper.checkIfURLExists(XmlDataParseHelper.java:172)

等 有谁知道发生了什么,以及如何解决这个问题? 谢谢

最佳答案

哦,我刚刚找到了答案,看来这是新版本 android 中的错误。添加此行对我有用:

httpUrlConn.setRequestProperty( "Accept-Encoding", "" ); 

谢谢

关于java - EOFException getResponseCode() 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20655317/

相关文章:

Java I/O 类和性能

java - Android和Java的JNI

java - 是否可以将 count(*) 和 groupBy 添加到 Spring Data Jpa 规范?

android - 带有类型参数的 Kotlin Poet super 接口(interface)

c++ - 如何将行号传递给异常?

c++ - C++ 中的 Winsock2 + OpenSSL : How can I catch their exceptions?

java - "not a multipath request"上传本地文件到服务器时出错

android - 使用更改外观后如何更新 Activity ?

c# - 如果从 IEnumerator<T>.MoveNext() 或 .Current 引发异常,是否仍必须处理该异常?

android - 错误:无法为根项目获取未知属性 'plugin'