1

我正在尝试使用 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",...
4

2 回答 2

2

我对 HttpClient 4.1.2 也有类似的问题。对我来说,它是通过恢复到 HttpClient 4.0.3 来解决的。无论是使用内置实现还是使用 JCIFS,我都无法让 NTLM 与 4.1.2 一起工作。

于 2011-10-22T00:39:50.557 回答
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 上运行时使用了特定于平台的调用

于 2011-05-25T13:48:16.127 回答