18

This small code snippet runs fine on my Mac's JVM. Unfortunately it crashes when executed on 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() + "" );
    }
}

If I replace https://github.com with https://www.facebook.com it works fine but I'm failing to figure out why.

The exception does not contain a message; so here's at least the stack trace.

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)
Taig
  • 6,718
  • 4
  • 44
  • 65

1 Answers1

41

Turned out this is a known bug in Android's class implementation. Calling Connection.setRequestProperty( "Accept-Encoding", "" ); before connecting can be used as workaround.

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

Taig
  • 6,718
  • 4
  • 44
  • 65
  • 8
    For future reference, that bug is marked as fixed in JB, but I'm getting exactly the same problem in KK. They've switched to an OkHttp-based implementation for KK, and it seems to have the same bug. Unfortunately, that bug is "restricted" in the tracker so I can't ask for it to be reopened. The same workaround still works. – Dan Hulme Jun 05 '14 at 12:53