IOException:“收到的身份验证质询为空” (阿帕奇和谐/安卓)

发布于 2024-08-03 15:05:39 字数 715 浏览 7 评论 0原文

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

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

此错误是什么意思,是什么导致了此错误?我正在将 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

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 IOException is thrown:

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

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.

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜扑 2024-08-10 15:05:39

我找到了原因。

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

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

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...).

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

要走干脆点 2024-08-10 15:05:39

也许对某人有用...

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

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).

亣腦蒛氧 2024-08-10 15:05:39

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

WWW-Authenticate: None

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

WWW-Authenticate: None
孤单情人 2024-08-10 15:05:39

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

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

当从服务器接收没有 WWW-Authenticate 标头的 HTTP 401 时,您将不会遇到问题场地。

Please note that there are two authentication approaches: HTTP Authentication and token-based authentication. If you are using HTTP Authentication then you have to follow referenced specification: include WWW-Authenticate header field on server side, use java.net.Authenticator locally, 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()

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文