本文介绍了发现无认证的挑战 - Android 4.3的HttpURLConnection类+基本认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个HttpURLConnection类基本身份验证来获取一些数据。直到4.2本工作就好了,但是4.3的的Nexus 4我得到的 java.io.IOException异常:未找到验证挑战的查询响应code时。奇怪的是,当我运行一个仿真器4.3在同一code没有出现问题。

我想在这里提到的解决方案,但没有运气:HTTP在Android果冻豆基本身份验证问题4.1使用HttpURLConnection类

code:

 网​​址URL =新的URL(urlString);
HttpURLConnection类的URLConnection =(HttpURLConnection类)url.openConnection();urlConnection.setRequestMethod(GET);
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(2000000);字符串为userpass =用户+:+通过;
串BASICAUTH;//从SO修复
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.JELLY_BEAN){
    BASICAUTH =基本+新的String(Base64.en codeToString(userpass.getBytes(),Base64.NO_WRAP));
}
其他{
 // Base64编码:http://iharder.net/base64
    BASICAUTH =基本+新的String(com.foo.bar.Base64.en codeBytes(userpass.getBytes()));
}
urlConnection.setRequestProperty(授权,BASICAUTH);//抛出异常
urlConnection.getResponse code();

追踪:

  09-01 21:40:13.501:W / System.err的(23055):java.io.IOException异常:未找到验证挑战
09-01 21:40:13.501:W / System.err的(23055):在libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:438)
09-01 21:40:13.501:W / System.err的(23055):在libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:418)
09-01 21:40:13.501:W / System.err的(23055):在libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:367)
09-01 21:40:13.501:W / System.err的(23055):在libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:301)
09-01 21:40:13.501:W / System.err的(23055):在libcore.net.http.HttpURLConnectionImpl.getResponse code(HttpURLConnectionImpl.java:497)
09-01 21:40:13.501:W / System.err的(23055):在com.foo.bar.InputStreamHelper.getUrlConnectionFromUrl(InputStreamHelper.java:65)


解决方案

的问题是通过提高构建目标API级别解决18

I am using Basic Authentication on a HttpURLConnection to fetch some data. Up until 4.2 this worked just fine, but with 4.3 on the Nexus 4 I get a java.io.IOException: No authentication challenges found when querying the response code. The strange thing is that the problem does not occur when I run the same code on a 4.3 emulator.

I tried the solution mentioned here, but no luck: HTTP Basic Authentication issue on Android Jelly Bean 4.1 using HttpURLConnection

Code:

URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(2000000);

String userpass = user + ":" + pass;
String basicAuth;

//fix from SO
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
    basicAuth = "Basic " + new String(Base64.encodeToString(userpass.getBytes(), Base64.NO_WRAP));
}
else {
 //Base64: http://iharder.net/base64
    basicAuth = "Basic " + new String(com.foo.bar.Base64.encodeBytes(userpass.getBytes()));
}
urlConnection.setRequestProperty ("Authorization", basicAuth);

//throws Exception
urlConnection.getResponseCode();

Trace:

09-01 21:40:13.501: W/System.err(23055): java.io.IOException: No authentication challenges found
09-01 21:40:13.501: W/System.err(23055):    at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:438)
09-01 21:40:13.501: W/System.err(23055):    at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:418)
09-01 21:40:13.501: W/System.err(23055):    at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:367)
09-01 21:40:13.501: W/System.err(23055):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:301)
09-01 21:40:13.501: W/System.err(23055):    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
09-01 21:40:13.501: W/System.err(23055):    at com.foo.bar.InputStreamHelper.getUrlConnectionFromUrl(InputStreamHelper.java:65)
解决方案

The problem was solved by raising the build target to API Level 18

这篇关于发现无认证的挑战 - Android 4.3的HttpURLConnection类+基本认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 06:04
查看更多