I'm using Apache HtppComponents (version - 4.5.2) and I'm trying to request HTTPS page via proxy server.
EDIT:
My main problem is that I need to know how to distinguish between failures of proxy server and failures of requested URIs (for both HTTP and HTTPS). I have a lot of proxies and they are not reliable 100%, so in case of proxy failure I need to retry request with different proxy server.
For example (in case of HTTPS), NoHttpResponseException
can be returned in both cases, ie when proxy failed or when target URL failed. How I can know the root of problem? From where came that NoHttpResponseException
? From proxy of from target URI?
I thought I can try to read response for CONNECT
request to proxy server, and if it is 200, then I it's means that proxy is good and next NoHttpResponseException
is come from target URL. But if I get NoHttpResponseException
immediately (before proxy returned me 200 status code for CONNECT), that means that problem with proxy itself, and I need to retry request with different proxy server. But I couldn't find any docs how to get access to response of CONNECT
request returned by proxy server.
Also, sometimes I'm getting HttpHostConnectException
and ConnectTimeoutException
. I'm treating this exceptions as problem with proxy (for both HTTP and HTTPS) - is it right way? Or this 2 exceptions can occur even for target URL?
It is not going to be pretty but it should do the trick
HttpHost myproxy = new HttpHost("myproxy", 8080)
CloseableHttpClient client = HttpClientBuilder.create()
.setProxy(myproxy)
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy() {
@Override
public boolean isAuthenticationRequested(HttpHost authhost, HttpResponse response, HttpContext context) {
if (myproxy.equals(authhost)) {
context.setAttribute("proxy.status", response.getStatusLine());
}
return super.isAuthenticationRequested(authhost, response, context);
}
})
.build();
HttpClientContext context = HttpClientContext.create();
HttpGet get = new HttpGet("https://httpbin.org/");
try (CloseableHttpResponse response = client.execute(get, context)) {
EntityUtils.consume(response.getEntity());
}
StatusLine proxyStatus = context.getAttribute("proxy.status", StatusLine.class);
System.out.println("Proxy said " + proxyStatus);
PS: HttpHostConnectException
and ConnectTimeoutException
exceptions can only be thrown on the first hop, that is, when connecting to the proxy. If the proxy fails to connect to the target server for whatever reason it is likely to respond with a 5xx status in response to the CONNECT method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.