http组件无法连接到代理

时间:2017-04-05 14:07:06

标签: java apache-httpclient-4.x apache-httpcomponents

我试图通过代理连接到网站,但我收到了错误

Error Code: 407 Proxy Authentication Required. Forefront TMG requires authorization to fulfill the request. Access to the Web Proxy filter is denied. (12209)

我的代码非常接近apache提供的示例https://hc.apache.org/httpcomponents-client-ga/examples.html(请参阅代理身份验证示例)。我确实在认证上做错了,但是......什么?

 HttpHost proxy = new HttpHost("http-proxy", 80);
    HttpHost target = new HttpHost(url, 80);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user,password));


try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(new SystemDefaultCredentialsProvider()).build()) {

RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet httpget = new HttpGet("/basic-auth/user/passwd");
httpget.setConfig(config);

HttpResponse response = client.execute(target, httpget);
}

1 个答案:

答案 0 :(得分:0)

问题似乎是在构建HTTP客户端时设置new SystemDefaultCredentialsProvider()。我想您的目的是设置credsProvider,您刚刚添加了代理用户和密码。