vojta
vojta

Reputation: 5651

HttpURLConnection - uknown format (magic number 376)

I call a HTTP GET request from my Android app:

String urlString = "http://my.service.com/foo";
HttpURLConnection conn = (HttpURLConnection) (new URL(urlString)).openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setRequestProperty("X-MY-HEADER", "1");
conn.setDoOutput(false);

int code = conn.getResponseCode();

The last line throws an exception:

java.io.IOException: unknown format (magic number 376)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:101)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
at com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)

HTTP engine seems to use gzip internally, which causes the exception. I didn't ask for any compression though. Why is this exception thrown? Can I turn it off somehow? What does this magic number 376 mean?

Upvotes: 0

Views: 201

Answers (1)

vojta
vojta

Reputation: 5651

I solved it! According to developer.android.com:

By default, this implementation of HttpURLConnection requests that servers use gzip compression and it automatically decompresses the data for callers of getInputStream(). The Content-Encoding and Content-Length response headers are cleared in this case. Gzip compression can be disabled by setting the acceptable encodings in the request header:

conn.setRequestProperty("Accept-Encoding", "identity");

Upvotes: 1

Related Questions