首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Httpcomponents-客户端代理问题

Httpcomponents-客户端代理问题
EN

Stack Overflow用户
提问于 2012-11-09 20:27:24
回答 1查看 867关注 0票数 0

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

如果我直接使用代理对象和HttpURLConnection,一切都很顺利:

代码语言:javascript
复制
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做同样的事情:

代码语言:javascript
复制
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();
}

但我得到的是:

代码语言:javascript
复制
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  ]

我也试过了

代码语言:javascript
复制
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
httpClient.getConnectionManager().getSchemeRegistry(),new MyProxySelector());  
    httpClient.setRoutePlanner(routePlanner);

其中MyProxySelector返回我创建的代理,但没有返回结果。

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

EN

回答 1

Stack Overflow用户

发布于 2013-01-09 21:47:29

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

看起来您需要添加下面这一行:

代码语言:javascript
复制
httpClient.getCredentialsProvider().setCredentials(new AuthScope("yourProxyHost", Port),
            new UsernamePasswordCredentials("yourUsername", "yourPass"));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13308073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档