Android的HttpURLConnection在HEAD请求上抛出EOFException [英] Android's HttpURLConnection throws EOFException on HEAD requests

查看:309
本文介绍了Android的HttpURLConnection在HEAD请求上抛出EOFException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个小代码片段可以在Mac的JVM上正常运行。不幸的是,它在Android 4.2上执行时崩溃。

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() + "" );
    }
}

如果我替换 https: //github.com https://www.facebook.com 它工作正常,但我没弄弄原因。

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)


推荐答案

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

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

这篇关于Android的HttpURLConnection在HEAD请求上抛出EOFException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆