问题描述
我有一些典型的代码,它们使用 HttpURLConnection 来获取带有 URL 的文件.它们在 android 1.x 和 2.x 中运行良好.但在 Android 4.1 中失败了!
I have some typical codes which used HttpURLConnection to get a file with an URL.They worked fine in android 1.x and 2.x. But failed in Android 4.1!
我在网上搜索,但几乎没有找到类似的信息.有人可以帮忙调查这个问题吗?
I searched on the web but found little similar information.Would anybody please help to investigate this issue?
private String mURLStr;
private HttpURLConnection mHttpConnection;
...
url = new URL(mURLStr);
...
mHttpConnection = (HttpURLConnection) url.openConnection();
mHttpConnection.setDoOutput(true);
mHttpConnection.setRequestMethod("GET");
...
InputStream is = mHttpConnection.getInputStream();
getInputStream 方法抛出异常:
The getInputStream method throws an exception:
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)
...
推荐答案
我目前面临同样的问题.在 4.1 Jelly Bean 上,我在 HttpURLConnection 上调用 getResponseCode() 时收到 IOExceptionNo authentication challenge found".
I am currently facing the same problem. On 4.1 Jelly Bean I receive an IOException "No authentication challenges found" when calling getResponseCode() on the HttpURLConnection.
我在网上搜索了一下Android源代码有什么变化,发现如下:4.0.4(工作):https:///bitbucket.org/seandroid/libcore/src/7ecbe081ec95/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java4.1.1(不工作):https://bitbucket.org/seandroid/libcore/src/6b27266a2856/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java
I have searched online to see what has changed in the Android source code and found the following:4.0.4 (working): https://bitbucket.org/seandroid/libcore/src/7ecbe081ec95/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java4.1.1 (not working): 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 为空,则抛出异常.
As one can see in 4.1 JB the method getAuthorizationCredentials() throws the IOException. It parses the challenge headers it finds in the response using HeaderParser.parseChallenges(..), if the response code is 401 or 407. If the returned List is empty the Exception is thrown.
我们目前正在调查究竟是什么导致该列表为空,但怀疑我们的服务器可能在挑战标头中使用了 realm=... 而不是 realm="...".缺少引号可能是导致此问题的原因.我们必须进一步调查情况是否确实如此,以及我们是否可以使其发挥作用.
We are currently investigating what exactly causes that List to be empty, but have the suspicion that our server might use realm=... instead of realm="..." in the challenge header. Missing quotation marks might be the cause for this problem. We have to investigate further if that is indeed the case and if we can make it work.
这篇关于HttpURLConnection 在 Android 2.x 中工作正常,但在 4.1 中无效:未发现身份验证挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!