【问题标题】:Httpcomponents-client proxy problemsHttpcomponents-客户端代理问题
【发布时间】:2012-11-09 12:27:23
【问题描述】:

我在尝试使用新的 httocomponent-client 模块通过代理连接到 Internet 时遇到问题

如果我直接使用 Proxy 对象和 HttpURLConnection 一切正常:

URL u = new URL("http://www.google.com");
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("somehost", 8080));
HttpURLConnection con = (HttpURLConnection) u.openConnection(proxy);
con.setRequestMethod("GET");
System.out.println(con.getResponseCode());

现在我尝试对新的 api 做同样的事情:

HttpHost proxy = new HttpHost("somehost", 8080, "http");
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpHost targetHost = new HttpHost("http://www.google.com");
HttpGet httpGet = new HttpGet("/");
try {
    HttpResponse httpResponse = httpClient.execute(targetHost, httpGet);
    System.out.println(httpResponse.toString());
} catch (Exception e) {
    e.printStackTrace();
}

但我明白了:

HTTP/1.1 407 Proxy Authentication Required ( Forefront TMG requires authorization to fulfill the request. Access to the Web Proxy filter is denied.  ) [Via: 1.1 xxx, 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: 7079  ]

我也试过了

ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
httpClient.getConnectionManager().getSchemeRegistry(),new MyProxySelector());  
    httpClient.setRoutePlanner(routePlanner);

其中 MyProxySelector 返回我已选择但没有结果的代理。

为什么使用新 API 需要在代码中进行代理身份验证?

【问题讨论】:

    标签: proxy apache-httpcomponents


    【解决方案1】:

    我不知道为什么使用 ProxySelectorRoutePlanner 的解决方案不起作用,您确定使用代理设置启动 JVM 吗?

    看来你需要添加这一行:

    httpClient.getCredentialsProvider().setCredentials(new AuthScope("yourProxyHost", Port),
                new UsernamePasswordCredentials("yourUsername", "yourPass"));
    

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 2013-11-08
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      相关资源
      最近更新 更多