【问题标题】:HttpsURLConnection Authenticator ErrorHttpsURLConnection 身份验证器错误
【发布时间】:2018-06-29 19:37:30
【问题描述】:

我在使用 Authenticator Android Method 时遇到问题。

此方法有效并返回 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;

有什么建议吗?

【问题讨论】:

    标签: java android authentication


    【解决方案1】:

    问题出在服务器上。

    认证方式为“Basic”。

    将其更改为“Digest”,服务器只尝试一次身份验证,而不是递归直到可以登录。

    编辑

    但要注意,因为

    HttpUrlConnection 不支持 here 指出的摘要

    【讨论】:

      猜你喜欢
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 2019-01-03
      • 2012-05-16
      • 2011-02-16
      • 2013-11-28
      • 2017-11-14
      相关资源
      最近更新 更多