使用 NTLM 身份验证时,最新 Apache HttpClient 4.1.1 出现 HTTP/1.1 407 错误

发布于 2024-11-09 17:53:51 字数 2659 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与风相奔跑 2024-11-16 17:53:51

我在 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.

屌丝范 2024-11-16 17:53:51

如果您对 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文