简体   繁体   English

org.apache.http.impl.client.CloseableHttpClient代理验证

[英]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.

相关问题 Android Studio:Unirest-Java找不到类“ org.apache.http.impl.client.CloseableHttpClient” - Android Studio: Unirest-Java Could not find class 'org.apache.http.impl.client.CloseableHttpClient' 当org.apache.http.impl.client.CloseableHttpClient返回404 / NOT_FOUND时,wget返回200 / OK - wget returns 200/OK while org.apache.http.impl.client.CloseableHttpClient returns 404/NOT_FOUND 使用org.apache.http.impl.client.ProxyClient通过代理隧道访问HTTPS Web服务 - Accessing HTTPS web services through proxy tunnel using org.apache.http.impl.client.ProxyClient 我如何告诉org.apache.http.impl.client.DefaultHttpClient使用OWASP Zed攻击代理(ZAP) - How do I tell org.apache.http.impl.client.DefaultHttpClient to use the OWASP Zed Attack Proxy (ZAP) org.apache.http.client-4.3.6:java.lang.NoClassDefFoundError:org.apache.http.impl.conn.PoolingHttpClientConnectionManager - org.apache.http.client-4.3.6: java.lang.NoClassDefFoundError: org.apache.http.impl.conn.PoolingHttpClientConnectionManager org.apache.http.impl.client.DefaultRequestDirector方法executeSB(Galaxy S5) - org.apache.http.impl.client.DefaultRequestDirector method executeSB (Galaxy S5) 配置org.apache.http.impl.client.DefaultHttpClient使用默认的Authenticator - Configuring org.apache.http.impl.client.DefaultHttpClient to use the default Authenticator java.lang.ClassNotFoundException:org.apache.http.impl.client.cache.CacheConfig - java.lang.ClassNotFoundException: org.apache.http.impl.client.cache.CacheConfig 如何在Jersey和Apache Http客户端上使用代理身份验证? - How to use Proxy authentication with Jersey and Apache Http client? 具有PHP,MySQL的Android登录注册系统,错误为“ java.lang.NoSuchMethodError:org.apache.http.impl.client.DefaultHttpClient.execute” - Android Login Registration System with PHP, MySQL with error 'java.lang.NoSuchMethodError: org.apache.http.impl.client.DefaultHttpClient.execute'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM