HttpURLConnection类没有找到在Android上的NTLM挑战
我想我的Android应用程序连接到使用的HttpURLConnection类IIS服务器。
I'm trying to connect my Android app to an IIS server using the HttpUrlConnection class.
我的服务器需要用户为进行验证,所以它发送下面挑战给客户端:
My server needs the user to be authenticate, so it is sending the following challenge to the client :
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
我的问题是HttpURLConnection的似乎并不解析它。所以的getPasswordAuthentication()永远不会调用,并返回一个IOException异常无身份验证的挑战找到了。
My problem is that HttpUrlConnection doesn't seems to parse it. So getPasswordAuthentication() is never call, and it return an IOException "no authentication challenges found".
下面是我的code:
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) {
...
}
我真的开始觉得NTLM挑战是不支持的HttpURLConnection。我看到一些图书馆似乎做的工作,但我想preFER不使用外部库。
I'm really starting to think that NTLM challenge is just not supported by HttpUrlConnection. I saw some libraries that seems to do the work, but I would prefer not to use external libraries.
有人可以确认是否是可以或不可以做的HttpURLConnection手柄NTLM挑战,而无需外部库?
Can somebody confirm if it is possible or not to make HttpUrlConnection handle NTLM challenge without external libs?
我只能够使通过设置AuthScheme及以下的图书馆用HttpClient的工作: http://jcifs.samba.org/src/jcifs-krb5-1.3.17.zip 。
I've only been able to make it work with HttpClient by setting the AuthScheme and the library below: http://jcifs.samba.org/src/jcifs-krb5-1.3.17.zip.
HttpClient httpclient = new HttpClient(httpParameters, context);
NTCredentials creds = new NTCredentials("username", "password", "", "dir");
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(context.getString("whatever is your main URL"), -1), creds);
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
然后你实现了JCIFS发动机和工厂。您可以在 http://hc.apache.org/httpcomponents-样本客户4.2.x版/ ntlm.html