在使用 Apaches HttpComponent 的 httpclient 时,我一直在尝试从配置的属性中配置代理身份验证的用户和密码,但没有成功。我发现的所有示例都指的是不再可用的方法和类,例如HttpState
和setProxyCredentials
。
那么,谁能给我一个如何配置代理凭据的示例?
在使用 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
并向其发送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();
}
对 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);