HttpURLConnection在Android 2.x中运行良好但在4.1中没有运行:没有发现身份验证挑战

fir*_*ear 30 authentication android httpurlconnection

我有一些典型的代码使用HttpURLConnection来获取带有URL的文件.他们在android 1.x和2.x中运行良好.但在Android 4.1中失败了!

我在网上搜索但发现的信息很少.有人请帮忙调查这个问题吗?

private String mURLStr; 
private HttpURLConnection mHttpConnection;

...

url = new URL(mURLStr);

...

mHttpConnection = (HttpURLConnection) url.openConnection();
mHttpConnection.setDoOutput(true);
mHttpConnection.setRequestMethod("GET");

...

InputStream is = mHttpConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)

getInputStream方法抛出异常:

08-01 15:56:48.856: W/System.err(13613): java.io.IOException: No authentication challenges found
08-01 15:56:48.856: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427)
08-01 15:56:48.866: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407)
08-01 15:56:48.866: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356)
08-01 15:56:48.866: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
08-01 15:56:48.866: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
...
Run Code Online (Sandbox Code Playgroud)

Hen*_*rik 27

我目前面临同样的问题.在4.1 Jelly Bean上,当在HttpURLConnection上调用getResponseCode()时,我收到IOException"未发现身份验证挑战".

我在线搜索了Android源代码中发生的变化,发现以下内容:4.0.4(工作):https://bitbucket.org/seandroid/libcore/src/7ecbe081ec95/luni/src/main/java/ libcore/net/http/HttpURLConnectionImpl.java 4.1.1(不工作):https://bitbucket.org/seandroid/libcore/src/6b27266a2856/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java

正如4.1 JB中所见,方法getAuthorizationCredentials()抛出IOException.如果响应代码为401或407,它会使用HeaderParser.parseChallenges(..)解析它在响应中找到的挑战标头.如果返回的List为空,则抛出异常.

https://bitbucket.org/seandroid/libcore/src/6b27266a2856/luni/src/main/java/libcore/net/http/HeaderParser.java

我们目前正在调查究竟是什么原因导致List为空,但怀疑我们的服务器可能在质询标头中使用realm = ...而不是realm ="...".缺少引号可能是导致此问题的原因.如果确实如此,我们必须进一步调查,如果我们能够使它发挥作用.

  • 我最后通过删除`mHttpConnection.setDoOutput(true);`使我的应用程序在android 4.x下工作.但是我仍然不清楚根本原因. (2认同)

Dan*_*lB6 26

根据RFC2617:

原始服务器使用401(未授权)响应消息来质询用户代理的授权.该响应必须包括WWW-Authenticate头字段,其中包含至少一个适用于所请求资源的质询.

在Android中,HttpURLConnection的getResponseCode()方法抛出java.io.IOException: No authentication challenges found当服务器返回或者是401 Unauthorized407 Proxy Authentication Required状态代码,而不WWW-Authenticate头集合.

如果您拥有服务器端API,那么您可以通过在返回401或407时添加所需的WWW-Authenticate标头来修复它.在我的情况下,我在PHP中修复它,如下所示:

header('WWW-Authenticate: OAuth realm="users"');
header('HTTP/1.1 401 Unauthorized');
Run Code Online (Sandbox Code Playgroud)


pet*_*ejl 6

我也有同样的问题.我发现这个解决方法,但它不适用于Android 2.在Jelly Bean上,它工作正常.只需使用getErrorStream()而不是getInputStream().

try
{
    responseStream = new BufferedInputStream(connection.getInputStream());
}
catch(IOException e)
{
    responseStream = new BufferedInputStream(connection.getErrorStream());
}
Run Code Online (Sandbox Code Playgroud)