我遇到了验证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;有小费吗?
发布于 2018-07-02 19:27:35
问题出在服务器上。
身份验证方法是"Basic“。
将其更改为"Digest“服务器只尝试一次身份验证,而不是递归地进行身份验证,直到可以登录。
编辑
但要注意,因为
HttpUrlConnection不支持尖头这里那样的摘要
https://stackoverflow.com/questions/51108360
复制相似问题