[英]Apache HttpClient 4.1 - Proxy Authentication
在使用 Apaches HttpComponent 的 httpclient 时,我一直在尝试从配置的属性中配置用于代理身份验证的用户和密码,但没有成功。 我发现的所有示例都引用了不再可用的方法和类,例如HttpState
和setProxyCredentials
。
那么,谁能给我一个如何配置代理凭据的示例?
对于任何寻找 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();
对于 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
来管理它。
可以在 4.3+ httpClient 上使用普通的旧用户名和密码代替 NTLM,如下所示:
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"));
此答案使用 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
并向http://www.google.com/some-path
发送GET
请求,并将打印响应 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();
}
对 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);
对于 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);
如果你必须让你的代码在 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.