6

我们正在使用 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 中没有:未发现身份验证挑战

4

2 回答 2

5

我们能够通过以下新方法解决 Jelly Bean 不调用 Authenticator 的 getPasswordAuthentication() 的问题:

@TargetApi(Build.VERSION_CODES.FROYO) 
private void setJellyBeanAuth(HttpURLConnection httpConn) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        byte[] auth = (USER + ":" + PASSWORD).getBytes();
        String basic = Base64.encodeToString(auth, Base64.NO_WRAP);
        httpConn.setRequestProperty("Authorization", "Basic " + basic);
    }
}

然后在打开连接后调用这个函数:

httpURLConn = (HttpURLConnection) url.openConnection();
setJellyBeanAuth(httpURLConn);

Froyo 注释的 @TargetApi 只是必要的,因为我们仍然支持 API 7,而在 API 8 中添加了 Base64

于 2013-01-27T19:49:00.100 回答
3

对于 Android 的 httpURLConnection 的 401 UNAUTHORIZED 响应,您的服务器必须发回这样的身份验证标头...
WWW-Authenticate: Bogus realm="blahblah", comment="use form to log in"
按照
https://www.rfc -editor.org/rfc/rfc2616 搜索“10.4.2 401 未授权”

于 2013-04-09T04:34:46.237 回答