Apache HttpClient 4.1 - Proxy Authentication

在使用Apaches HttpComponent的httpclient时,我一直在尝试从配置的属性中配置用户和密码以进行代理身份验证,但没有成功。我找到的所有示例都引用了不再可用的方法和类,例如 和 。HttpStatesetProxyCredentials

那么,任何人都可以给我一个如何配置代理凭据的示例吗?


答案 1

对于任何寻找4.3答案的人来说......它相当新,他们的例子没有使用新的HttpClientBuilder......所以这就是我在那个版本中实现这个的方式:

NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword,localMachineName, domainName );

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds );
HttpClientBuilder clientBuilder = HttpClientBuilder.create();

clientBuilder.useSystemProperties();
clientBuilder.setProxy(new HttpHost(pxInfo.getProxyURL(), pxInfo.getProxyPort()));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

CloseableHttpClient client = clientBuilder.build();

答案 2

对于Basic-Auth,它看起来像这样:

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("PROXY HOST", 8080),
    new UsernamePasswordCredentials("username", "password"));

HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https");
HttpHost proxy = new HttpHost("PROXY HOST", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

AFAIK NTLM 不是开箱即用的。但是,您可能能够使用来管理它,并且可能会过载 。NTCredentialsDefaultProxyAuthenticationHandler


推荐