【发布时间】:2012-12-12 13:40:22
【问题描述】:
我想使用 NTLM 为 DefaultHttpClient 实施抢先式身份验证。我从Dan Hounshell 中找到了一个库,它适用于正常的身份验证。
但我无法弄清楚如何使用抢先式身份验证进行这项工作。我用这个很酷的answer找到了问题Preemptive Basic authentication with Apache HttpClient 4:
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
HttpRequest request = ...
request.addHeader(new BasicScheme().authenticate(creds, request));
不适合我的问题,但一个好主意。所以我尝试使用NTLMSchemeFactory 创建AuthScheme 的新实例,并提供authenticate 函数。
NTCredentials ntc = new NTCredentials("example.com/user:pwd");
httpPost.addHeader(new NTLMScheme(new JCIFSEngine()).authenticate(ntc, httpPost));
当这个函数被调用时,我会得到一个异常:
org.apache.http.auth.AuthenticationException:意外状态:UNINITIATED
我该如何解决这个问题?
POST /login/ HTTP/1.1
Content-Length: 21
Content-Type: application/x-www-form-urlencoded
Host: example.com
Connection: Keep-Alive
data=...
HTTP/1.1 401 Unauthorized
Content-Type: text/html
Server: Microsoft-IIS/7.5
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Date: Wed, 12 Dec 2012 14:36:26 GMT
Content-Length: 1344
Much data...
POST /login/ HTTP/1.1
Content-Length: 21
Content-Type: application/x-www-form-urlencoded
Host: example.com
Connection: Keep-Alive
Authorization: NTLM AAABBBCCC...FFF==
data=...
我认为第一个请求绝对没用。
【问题讨论】:
标签: android authorization ntlm apache-httpclient-4.x preemptive