使用 NTLM 身份验证时,最新 Apache HttpClient 4.1.1 出现 HTTP/1.1 407 错误
我正在尝试使用 Apache HttpClient 4.1.1 库 (http ://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html)从我公司的代理后面访问站点,该代理使用带有 NTLM 身份验证的 ISA 服务器,但我不断收到HTTP 407 代理身份验证所需错误:
代码片段
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",...
I'm trying to use the Apache HttpClient 4.1.1 library (http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html) to access sites from behind my company's proxy that uses the ISA Server with NTLM authentication but I keep getting an HTTP 407 Proxy Authentication Required error:
Code Snippet
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 ----------------------------------------
What am I missing here?
Update:
In the same environment, code using the JDK URL and URLConnection classes works!
Working Code Snippet
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",...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 HttpClient 4.1.2 中遇到了类似的问题。对我来说,通过恢复到 HttpClient 4.0.3 解决了这个问题。我永远无法使用内置实现或使用 JCIFS 让 NTLM 与 4.1.2 一起工作。
I had a similar problem with HttpClient 4.1.2. For me, it was resolved by reverting to HttpClient 4.0.3. I could never get NTLM working with 4.1.2 using either the built-in implementation or using JCIFS.
如果您对 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 上运行
If you have no issues with LGPL licensed software you can try using NTLM engine developed by the Samba JCIFS project instead of the internal one used by Apache HttpClient per default.
See this document for detailed instructions:
http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/src/site/apt/ntlm.apt
PS: JDK URL and URLConnection classes work because they make use of platform specific calls when running on Microsoft Windows