我正在尝试使用Apache HttpClient 4.1.1库(http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html)从我公司的代理后面访问网站,该代理使用带有NTLM身份验证的ISA Server,但我一直收到HTTP 407 Proxy Authentication Required错误:
代码段
HttpHost proxy = new HttpHost("myProxyHost", 80, "http");
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
NTCredentials creds = new NTCredentials("myWindowsUserName", "myWindowsPwd", "localhost", "myCompanyDomain");
AuthScope authScope = new AuthScope("myProxyHost", 80, "", "NTLM");
httpClient.getCredentialsProvider().setCredentials(authScope, creds);
HttpHost target = new HttpHost("www.google.com", 80, "http");
HttpGet get = new HttpGet("/");
System.out.println("executing request to " + target + " via " + proxy);
HttpResponse rsp = httpClient.execute(target, get);
System.out.println("----------------------------------------");
System.out.println(rsp.getStatusLine());
Header[] headers = rsp.getAllHeaders();
for (int i = 0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
O / P
executing request to http://www.google.com:80 via http://myProxyHost:80 ---------------------------------------- HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. ) Via: 1.1 myCompanyServer Proxy-Authenticate: Negotiate Proxy-Authenticate: Kerberos Proxy-Authenticate: NTLM Connection: Keep-Alive Proxy-Connection: Keep-Alive Pragma: no-cache Cache-Control: no-cache Content-Type: text/html Content-Length: 4120 ----------------------------------------
我在这里缺少什么?
更新: 在相同的环境中,使用JDK URL和URLConnection类的代码可以工作!
工作代码段
System.setProperty("http.proxyHost", "myProxyHost");
System.setProperty("http.proxyPort", "80");
URL url = new URL("http://www.google.com");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
O / P
Google window.google={kEI:"_N3cTaLFMY6cvgOH9MypDw",...
答案 0 :(得分:2)
我遇到了与HttpClient 4.1.2类似的问题。对我来说,通过恢复到HttpClient 4.0.3解决了这个问题。使用内置实现或使用JCIFS,我永远无法让NTLM使用4.1.2。
答案 1 :(得分:1)
如果您对LGPL许可软件没有任何问题,您可以尝试使用由Samba JCIFS项目开发的NTLM引擎,而不是默认情况下Apache HttpClient使用的内部引擎。
有关详细说明,请参阅此文档:
http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/src/site/apt/ntlm.apt
PS:JDK URL和URLConnection类可以工作,因为它们在Microsoft Windows上运行时使用特定于平台的调用