我们正在使用 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
我们在 Android 文档中用于 HttpURLConnection 的身份验证代码:
private void doAuthorize() {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER, PASSWORD.toCharArray());
}
});
}
经过进一步调查和故障排除,我们发现在 4.1 Jelly Bean 中没有调用此代码!
Android Jelly Bean 4.1 中基本身份验证的解决方法或正确方法是什么?
有人在这个相关主题的 Android 源代码中发现了不同,我认为我们遇到的问题与该差异有关:HttpURLConnection 在 Android 2.x 中运行良好,但在 4.1 中没有:未发现身份验证挑战