【发布时间】:2014-11-03 09:44:04
【问题描述】:
我们已经搜索和搜索,但似乎仍然无法解决此问题。我们有一个 REST Web 服务,并正在尝试从我们的 Android 应用程序访问它。从 Web 浏览器或 Chrome 中的 Advanced Rest Client 扩展访问 Web 服务 URL 时有效。我们的网址如下所示:
http://10.52.1.1:8080/GPService/Tenants(Name=DefaultTenant)/Companies(Fabrikam,%20Inc.)/Items(128%20SDRAM).JSON
一旦将其输入浏览器,系统就会提示我们输入凭据。输入凭据后,返回 JSON 结果。
在我们的 Android 应用中,我们正在尝试以下操作:
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("domain\\user", "password".toCharArray());
}
});
URL url = new URL(urlstring);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(false);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setConnectTimeout(60000);
InputStream istream = conn.getInputStream();
任何想法我们做错了什么?我们尝试了不同的方法,类似于Connecting to remote URL which requires authentication using Java 和http://www.muneebahmad.com/index.php/archives/127,但完全没有成功,我们不断收到 401 Unauthorized 错误。
根据收到的评论:我们的服务正在使用 NTLM 身份验证。
感谢您的帮助!
【问题讨论】:
-
您的 Web 服务使用哪种身份验证?形式还是基本?
-
@JunedAhsan 我们实际上正在使用 NTLM 身份验证。在原始问题中添加了该详细信息。
-
您在这里查看答案了吗:stackoverflow.com/questions/20505207/…。 Android 实际上没有执行 NTLM 的 Authenticator 的具体实现。
-
@ErikNedwidek 我们确实看过那个例子,但由于某种原因,它看起来甚至没有进入代码的 Authenticator 部分。我们在 getPasswordAuthentication() 方法中放置了一个 Print 语句,并且它使用了我们甚至没有命中该语句/断点的调试器。
-
点头。是的,我相信这是可以预料的。 PasswordAuthentication 只是用户/密码对的持有者。 Authenticator 的具体实现在需要时调用 getPassword。当 HttpUrlConnection 对象看到 WWW-Authenticate: NTLM 标头时,它意识到它没有该方法的具体实现并将连接保留为 401。实际上重新阅读文档,它说 Android HttpUrlConnection 仅支持基本身份验证。
标签: java android json http http-headers