我正在尝试使用 Apache HttpClient 4.1.1 库 ( http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html ) 从我公司使用 ISA 的代理访问网站具有 NTLM 身份验证的服务器,但我不断收到 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("----------------------------------------");
输出/输出
通过 http://myProxyHost:80 执行对 http://www.google.com:80 的请求 -------------------------------------- 需要 HTTP/1.1 407 代理身份验证(ISA 服务器需要授权才能完成请求。对 Web 代理过滤器的访问被拒绝。) 通过:1.1 myCompanyServer 代理验证:协商 代理验证:Kerberos 代理验证:NTLM 连接:保持活动 代理连接:保持活动 Pragma:无缓存 缓存控制:无缓存 内容类型:文本/html 内容长度: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();
输出/输出
谷歌 window.google={kEI:"_N3cTaLFMY6cvgOH9MypDw",...