【问题标题】:HTTP/1.1 407 error with latest Apache HttpClient 4.1.1 when using NTLM authentication使用 NTLM 身份验证时最新的 Apache HttpClient 4.1.1 出现 HTTP/1.1 407 错误
【发布时间】:2011-09-01 14:07:52
【问题描述】:

我正在尝试使用 Apache HttpClient 4.1.1 库 (http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html) 从我公司的代理后面访问站点,该代理使用带有 NTLM 身份验证的 ISA 服务器,但我不断收到 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

通过 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();

O/P

谷歌 window.google={kEI:"_N3cTaLFMY6cvgOH9MypDw",...

【问题讨论】:

    标签: java proxy ntlm apache-httpclient-4.x


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果您对 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 上运行时使用了特定于平台的调用

      【讨论】:

      • (jcifs.samba.org/src/docs/ntlmhttpauth.html) 表示不再支持。无论如何,我希望 HttpClient 就足够了,因为它的文档现在声称完全支持 NTLM。这就是我认为我的代码有问题的原因。
      • Jespa,jCIFS 网站上提到的替代方案看起来很有趣。
      • @kodeninja 这是对 JCIFS 的常见误解。弃用的是它们的 HTTP 相关代码,而不是库本身
      猜你喜欢
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      相关资源
      最近更新 更多