java IOException:“收到的身份验证质询为空”(Apache Harmony/Android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1357372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:14:49  来源:igfitidea点击:

IOException: "Received authentication challenge is null" (Apache Harmony/Android)

javaandroidhttpurlconnection

提问by Matthias

I am trying to send a GET via Android's HttpURLConnection(imported from org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection), and upon receiving the response, an IOExceptionis thrown:

我正在尝试通过 Android 的HttpURLConnection(从 导入org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection)发送 GET ,并在收到响应后抛出IOException

in doRequestInternal(): "Received authentication challenge is null"

在 doRequestInternal() 中:“收到的身份验证质询为空”

What does this error mean, and what is causing this? I am writing OAuth parameters to the Authorization header, but I do this on other occasions, too, without problems.

这个错误是什么意思,是什么导致了这个错误?我正在将 OAuth 参数写入 Authorization 标头,但我在其他场合也这样做,没有问题。

    if (connection == null) {
        connection = (HttpURLConnection) new URL(endpointUrl).openConnection();
        connection.setRequestMethod("GET");
    }

    //... do some OAuth message signing

    connection.connect();

    int statusCode = connection.getResponseCode(); // throws IOException

回答by Matthias

I found out the reason.

我发现了原因。

First of all, to all who aren't aware of what this error means (I sure wasn't): This exception is thrown if the server replies with a 401. Very intuitive, considering that it was thrown in getResponseCode() (i.o.w. you are never able to check for 401s yourself, but have to catch this IOException instead...).

首先,对于所有不知道此错误意味着什么的人(我确定没有):如果服务器回复 401,则会抛出此异常。非常直观,考虑到它是在 getResponseCode() 中抛出的(低您永远无法自己检查 401,但必须改为捕获此 IOException...)。

The actual cause for the 401 was that I didn't send an OAuth verifier code where it was expected at this point.

401 的实际原因是我没有在此时预期的地方发送 OAuth 验证器代码。

回答by Vladimir

Maybe will be useful for somebody...

也许会对某人有用......

This exception just means malformed answer headers: the "WWW-Authenticate" header was not found. Also, chunked answers with 401 code are not supported, so you'll need "Content-Length" header (can be zero).

此异常仅意味着格式错误的答案标头:未找到“WWW-Authenticate”标头。此外,不支持带有 401 代码的分块答案,因此您需要“Content-Length”标头(可以为零)。

回答by Corbella

Just add this header to the request (in server side):

只需将此标头添加到请求中(在服务器端):

WWW-Authenticate: None

回答by Grigori A.

Please note that there are two authentication approaches: HTTP Authenticationand token-based authentication. If you are using HTTP Authentication then you have to follow referenced specification: include WWW-Authenticateheader field on server side, use java.net.Authenticatorlocally, etc. If you are using token-based authentication then obviously you have to use cookies to store the token and make able to keep long lived sessions alive. In such case put the next code into android.app.Application.onCreate()

请注意,有两种身份验证方法:HTTP 身份验证基于令牌的身份验证。如果您使用 HTTP 身份验证,那么您必须遵循参考规范:在服务器端包含WWW-Authenticate标头字段,在java.net.Authenticator本地使用等。如果您使用基于令牌的身份验证,那么显然您必须使用 cookie 来存储令牌并制作能够保持长时间的会话活动。在这种情况下,将下一个代码放入android.app.Application.onCreate()

CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

and you won't have troubles when receiving HTTP 401 from the server without WWW-Authenticateheader field.

并且在没有WWW-Authenticate标头字段的情况下从服务器接收 HTTP 401 时不会遇到麻烦。