17

这个小代码片段在我的 Mac 的 JVM 上运行良好。不幸的是,它在 Android 4.2 上执行时会崩溃。

import java.net.HttpURLConnection;
import java.net.URL;

public class App
{
    public static void main( String... arguments ) throws Exception
    {
        HttpURLConnection connection = (HttpURLConnection) new URL( "https://github.com" ).openConnection();
        connection.setRequestMethod( "HEAD" );

        System.out.println( connection.getResponseCode() + "" );
    }
}

如果我https://github.com用它代替https://www.facebook.com它可以正常工作,但我无法弄清楚为什么。

异常不包含消息;所以这至少是堆栈跟踪。

java.io.EOFException
        at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:206)
        at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98)
        at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
        at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:541)
        at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:844)
        at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
        at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
        at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
4

1 回答 1

40

原来这是 Android 类实现中的一个已知错误。在连接之前调用Connection.setRequestProperty( "Accept-Encoding", "" );可以用作解决方法。

https://code.google.com/p/android/issues/detail?id=24672

于 2013-07-14T10:44:40.857 回答