我've been trying to configure the user and password for proxy authentication from the configured properties while using Apaches HttpComponent'的httpclient,但没有成功 . 我找到的所有示例都引用了不再可用的方法和类,例如 HttpState 和 setProxyCredentials .
那么,有人能举例说明如何配置代理凭证吗?
6 回答
0
对于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);
The following example will authenticate username:password@proxy.mycompany.com:8080 and send a GET request to http://www.google.com/some-path and will print the response HTTP code.
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();
}
23
对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);
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 回答
对于Basic-Auth,它看起来像这样:
开箱即用不支持AFAIK NTLM . 但是您可以使用
NTCredentials
来管理它,并且可能会重载DefaultProxyAuthenticationHandler
.对于任何寻找4.3的答案的人来说...它相当新,他们的例子没有使用新的HttpClientBuilder ......所以我在这个版本中实现了这个:
可以使用4.3 httpClient上的普通旧用户名和密码代替NTLM,如下所示:
如何使用Apache的httpclient设置代理身份验证
(代理网络上的预授权)
这个答案使用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) .The following example will authenticate username:password@proxy.mycompany.com:8080 and send a GET request to http://www.google.com/some-path and will print the response HTTP code.
对NTLM来说,一件简单的事情对我有用:
对于HttpClient 4.5和每个请求身份验证: