我一直在尝试使用Apaches HttpComponent的httpclient从配置的属性配置代理身份验证的用户和密码,但没有成功。我找到的所有示例都引用了不再可用的方法和类,例如HttpState
和setProxyCredentials
。
那么,有人能举例说明如何配置代理凭证吗?
答案 0 :(得分:37)
对于那些寻找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();
答案 1 :(得分:23)
对于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。但是您可以使用NTCredentials
来管理它,并可能会重载DefaultProxyAuthenticationHandler
。
答案 2 :(得分:12)
而不是NTLM,可以在4.3+ httpClient上使用普通的旧用户名和密码,如下所示:
HttpHost proxy = new HttpHost("x.x.com",8080);
Credentials credentials = new UsernamePasswordCredentials("username","password");
AuthScope authScope = new AuthScope("x.x.com", 8080);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(authScope, credentials);
HttpClient client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
HttpResponse response=client.execute(new HttpGet("http://stackoverflow.com/questions/6962047/apache-httpclient-4-1-proxy-authentication"));
答案 3 :(得分:5)
这个答案使用了Apache的HttpClient v4.1及更高版本。
接受的答案对我不起作用,但我找到了其他的东西!
以下是来自apache的一些经过测试验证的代码,演示了如何通过代理对HTTP请求进行身份验证。
完整文档位于此处:https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html。
Apache也有一个很好的例子:https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java
my_username
替换为您的代理用户名my_password
替换为您的代理密码proxy.mycompany.com
替换为您的代理主机8080
替换为您的代理端口google.com
替换为您要将HTTP请求发送到的网站的主机。/some-path
替换为您要向其发送HTTP请求的路径。这使用您之前指定的主机站点(google.com)。 以下示例将对username:password@proxy.mycompany.com:8080
进行身份验证并向GET
发送http://www.google.com/some-path
请求,并打印响应HTTP代码。
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("proxy.mycompany", 8080),
new UsernamePasswordCredentials("my_username", "my_password"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
try {
//Replace "google.com" with the target host you want to send the request to
HttpHost target = new HttpHost("google.com", 80, "http");
HttpHost proxy = new HttpHost("proxy.mycompany", 8080);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
CloseableHttpResponse response = null;
//Replace "/some-path" with the path you want to send a get request to.
HttpPost httppost = new HttpPost("/some-path");
httppost.setConfig(config);
response = httpclient.execute(target, httppost);
try {
System.out.println("Return status code is "+response.getStatusLine().getStatusCode());
} finally {
response.close();
}
} finally {
httpclient.close();
}
答案 4 :(得分:0)
对于NTLM来说,一件简单的事情对我有用:
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(proxy_host, proxy_port),
new NTCredentials(this.proxy_user, this.proxy_pass, this.proxy_host, this.proxy_domain));
HttpHost proxy = new HttpHost(this.proxy_host, this.proxy_port, "http");
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
答案 5 :(得分:0)
对于HttpClient 4.5和每个请求的身份验证:
HttpContext httpContext = new BasicHttpContext();
AuthState authState = new AuthState();
authState.update(new BasicScheme(), new UsernamePasswordCredentials("userName", "password"));
httpContext.setAttribute(HttpClientContext.PROXY_AUTH_STATE, authState);
CloseableHttpResponse httpResponse = httpClient.execute(httpRequest, httpContext);
答案 6 :(得分:0)
如果您必须保持代码与4.1一起使用或要使用以下代码段,那么一定要知道httpclient 4.1不会将身份验证发送给代理,这一点很重要。您可能会得到407“需要代理身份验证”状态码。我升级到4.3.3 ,尽管在此版本中不推荐使用DefaultHttpClient和ConnRoutePNames,但它们都运行良好。希望这会有所帮助!
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("PROXY HOST", 8080),
new UsernamePasswordCredentials("username", "password"));
HttpHost proxy = new HttpHost("PROXY HOST", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);