使用SSL加密和NTLM身份validation的HttpClient失败

我正在尝试在使用SSL加密(https)的Sharepoint 2010服务器上执行简单的REST调用,以及NTLM身份validation。 当服务器设置为不需要SSL(仅用于测试,服务器将需要生产中的SSL)时,我的NTLM身份validation和后续REST调用可以正常使用HttpClient。 但是,启用SSL后,身份validation将不再有效。

这是SSL处理的代码(设置为接受所有证书):

SSLContext sslContext = SSLContext.getInstance("TLS"); // set up a TrustManager that trusts everything sslContext.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }, new SecureRandom()); SSLSocketFactory sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme httpsScheme = new Scheme("https", 443, sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry); 

接下来是执行NTML和HTTP GET调用的代码:

  HttpParams params = new BasicHttpParams(); DefaultHttpClient httpclient = new DefaultHttpClient(cm, params); // Set up the NTLM credentials NTCredentials creds = new NTCredentials(USERNAME, PASSWORD, MACHINE, DOMAIN); httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds); // Setup the HTTP GET and execute it HttpHost target = new HttpHost(HOSTNAME, 443, "https"); HttpContext localContext = new BasicHttpContext(); HttpGet httpget = new HttpGet("/site/_vti_bin/ListData.svc/TestList"); HttpResponse response1 = httpclient.execute(target, httpget, localContext); 

我总是得到的回应是:

  HTTP/1.1 400 Bad Request The server encountered an error processing the request. See server logs for more details. 

请注意,我已经使用FireFox Poster和CURL来执行我正在尝试以编程方式执行的操作,并且它工作正常。 所以服务器似乎设置正确。

谢谢你的时间!

将此文档记录给可能遇到此问题的其他人。

问题是IIS服务器设置为允许匿名登录。 显然,有一个错误,如果设置了此错误,那么在该安装下与sharepoint实例的任何REST或SOAP连接都将不会成功。 似乎字符串“anonymous”被添加到REST / SOAP URL中,这当然使其指向不存在的服务。 在我们关闭允许匿名登录IIS实例后,调用工作。

我的猜测是由于SSL连接不持久,NTLM身份validation失败。 NTLM方案需要多次消息交换,并且只能使用持久连接。

如果您发布HTTP会话的连线/上下文日志(此处或httpclient-users列表),我应该能够说明为什么SSL连接不会停留。