繁体   English   中英

HttpsURLConnection Authenticator错误

[英]HttpsURLConnection Authenticator Error

我在Authenticator Android方法中遇到麻烦。

此方法有效并返回200代码( HTTP_OK ):

Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("admin", "1234".toCharArray());
                }
            });

但要变得有点生气,我向用户发送了值:

urlConnection = new URL(params[0]);
user = params[1];
pass = params[2];

将身份验证更改为:

return new PasswordAuthentication(user, pass.toCharArray())

做一个测试:用户/通过:“测试”。

我想收到401代码( HTTP_UNAUTHORIZED

当我调试并单击f7 / f8时,有时我可以在响应代码中看到401,但我无法返回它,因为似乎他进入了一个循环:

httpUrlConnection.getResponseCode();

这是我的代码的一部分:

.....    

    httpUrlConnection.setConnectTimeout(2000);
    httpUrlConnection.connect();
    statusCode = httpUrlConnection.getResponseCode(); //debug stop here
    httpUrlConnection.disconnect(); 
} catch (IOException e) {
    e.printStackTrace();
}
return statusCode;

有小费吗?

问题出在服务器上。

身份验证方法为“ Basic ”。

将其更改为“ Digest ”,服务器仅尝试一次身份验证,直到可以登录才递归进行。

编辑

但是要注意,因为

HttpUrlConnection不支持此处指出的摘要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM