[英]org.apache.http.impl.client.CloseableHttpClient Proxy Authentication
My application does https requests to different targets and now I have a proxy problem. 我的应用程序对不同的目标执行https请求,现在我遇到了代理问题。
While the client is connecting to target, I get an 407 (Proxy Authentication Required) from the target server. 当客户端连接到目标时,我从目标服务器获得407(需要代理身份验证)。 To be clear: Client reaches other servers in www already. 需要说明的是:客户端已经到达www中的其他服务器。
How can I build the CloseableHttpClient
in general to allow this proxy auth? 我如何构建一般的CloseableHttpClient
以允许此代理身份验证? Can someone give me a short example how to allow proxy auth? 有人可以给我一个简短的例子,如何允许代理身份验证?
Does double proxy auth (my proxy + external proxy) also works? 双代理身份验证(我的代理+外部代理)是否也有效?
See "Request configuration" section here . 请参阅此处的 “请求配置”部分。 In short: 简而言之:
1.) Build your client: 1.)建立你的客户:
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();
CloseableHttpClient httpclient = HttpClients.custom()
.(settingXY)
.setDefaultCookieStore(defaultCookieStore)
.setDefaultCredentialsProvider(defaultCredentialsProvider)
.setDefaultRequestConfig(defaultRequestConfig)
.setDefaultRequestConfig(defaultRequestConfig)
.build();
//You dont need to specify proxy here!!!
2.) Then build your reqeuest(s) like this: 2.)然后像这样构建你的需求:
HttpGet httpget = new HttpGet("http://www.apache.org/");
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setProxy(new HttpHost("myproxy", 8080))
.build();
httpget.setConfig(requestConfig);
3.) Then 3.)然后
defaultCredentialsProvider.setCredentials(new AuthScope(proxy.getHostName(), proxy.getPort()), proxyCredentials);
HttpGet httpget = new HttpGet("http://www.apache.org/");
HttpUriRequest request= httpget;
CloseableHttpResponse response = httpclient.execute(request, context);
Hope this helps someone. 希望这有助于某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.