我正在尝试从服务器注销.但是它会返回"0"响应代码并带有此异常.我正在使用GET动词来做到这一点.
logcat的
10-17 14:54:13.261: W/System.err(868): java.io.IOException: Received authentication challenge is null
10-17 14:54:13.284: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:397)
10-17 14:54:13.284: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:345)
10-17 14:54:13.304: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:276)
10-17 14:54:13.324: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
10-17 14:54:13.324: W/System.err(868): at com.remote.synchronizer.haris.CustomHttpClient.executeGet(CustomHttpClient.java:131)
10-17 14:54:13.354: W/System.err(868): at com.remote.synchronizer.haris.OptionsActivity$1$3$1.run(OptionsActivity.java:87)
10-17 14:54:13.364: W/System.err(868): at android.os.Handler.handleCallback(Handler.java:605)
10-17 14:54:13.384: W/System.err(868): at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 14:54:13.384: W/System.err(868): at android.os.Looper.loop(Looper.java:137)
10-17 14:54:13.404: W/System.err(868): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-17 14:54:13.424: W/System.err(868): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 14:54:13.424: W/System.err(868): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 14:54:13.454: W/System.err(868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 14:54:13.474: W/System.err(868): …Run Code Online (Sandbox Code Playgroud) 我们正在使用HTTP基本身份验证向Web服务器发出基于HttpURLConnection的请求.代码在Android版本2.x,3.x.,4.0.x上运行良好现在使用Jelly Bean和v4.1.x身份验证失败,LogCat中包含以下消息:
01-27 10:54:18.886: ...::doReadRawData(731): An exception occured while reading data from remote host. httpURLConn.responseCode = 401 / httpURLConn.responseMessage = UNAUTHORIZED
01-27 10:54:18.886: ...::doReadRawData(731): java.io.IOException: No authentication challenges found
Run Code Online (Sandbox Code Playgroud)
我们在Android文档中使用HttpURLConnection的身份验证代码:
private void doAuthorize() {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER, PASSWORD.toCharArray());
}
});
}
Run Code Online (Sandbox Code Playgroud)
经过进一步调查和排除故障后,我们发现4.1 Jelly Bean中没有调用此代码!
Android Jelly Bean 4.1中基本身份验证的解决方法或正确方法有哪些?
有人发现这个相关主题的Android源代码有所不同,我认为我们遇到的问题与这个差异有关:HttpURLConnection在Android 2.x中运行良好,但在4.1中没有运行:没有发现身份验证挑战
authentication android httpurlconnection basic-authentication android-4.2-jelly-bean
在Android 5.0及更高版本中运行时,我的代码运行正常.但是在Android 4.1.1中它会抛出java.net.SocketTimeoutException:SSL握手超时.
URL url;
HttpURLConnection connection = null;
String charset = "UTF-8";
String accessToken = "";
try {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("client_id", CLIENT_ID));
postParameters.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
postParameters.add(new BasicNameValuePair("grant_type", CLIENT_CREDENTIALS_GRANT_TYPE));
//Create connection
url = new URL(ACCESS_TOKEN_URL);
connection = (HttpURLConnection)url.openConnection();
connection.setReadTimeout( 10000 /*milliseconds*/ );
connection.setConnectTimeout( 15000 /* milliseconds */ );
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(getQuery(postParameters).getBytes(charset));
wr.flush();
wr.close();
int responseCode = connection.getResponseCode();
InputStream is = null;
if(responseCode==HttpStatus.SC_OK) …Run Code Online (Sandbox Code Playgroud) 我需要一个响应代码,但它抛出一个IOException.我不知道发生了什么事!
try
{
url = new URL(urlBuilder.toString());
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setConnectTimeout(TIME_OUT);
conn.setRequestMethod(METHOD_GET);
conn.setRequestProperty("accept", "*/*");
conn.connect();
int responseCode = conn.getResponseCode(); //throw IOException:Received authentication challenge is null
if (responseCode == HTTP_OK)
{
inStream = conn.getInputStream();
response = getResponse(inStream);
}
else
{
response = "response code?"+responseCode;
}
} catch (Exception e)
{
throw e;
}
finally
{
conn.disconnect();
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
IOException是:
05-03 20:14:01.577: WARN/System.err(1515): java.io.IOException: Received authentication challenge is null
05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1694)
05-03 20:14:01.577: INFO/QQWeiBo(1515): Received …Run Code Online (Sandbox Code Playgroud)