我正在尝试使用HttpUrlConnection类将我的Android应用程序连接到IIS服务器.
我的服务器需要用户进行身份验证,因此它向客户端发送以下质询:
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
Run Code Online (Sandbox Code Playgroud)
我的问题是HttpUrlConnection似乎没有解析它.因此,getPasswordAuthentication()永远不会调用,并返回IOException"未发现身份验证质询".
这是我的代码:
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myUsername", "myPassword".toCharArray());
}
});
URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Encoding", "gzip");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "close");
conn.setDoOutput(true);
conn.setDoInput(true);
try
{
conn.connect();
status_code = conn.getResponseCode();
}catch (IOException e) {
...
}
Run Code Online (Sandbox Code Playgroud)
我真的开始认为HttpUrlConnection不支持NTLM挑战.我看到一些图书馆似乎在做这项工作,但我不想使用外部库.
有人可以确认是否有可能在没有外部库的情况下使HttpUrlConnection处理NTLM挑战?