【发布时间】: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