I'm experiencing a problem with a simple program using Apache's Java HttpClient in which all requests sent from proxies time out (java.net.ConnectException). I have made sure that all of the proxies in the list work, so that is not the problem. Here's my code:
HttpHost proxy = new HttpHost(ip, port, "http"); // the vars ip and port are taken from the function this is in
DefaultHttpClient httpclient = new DefaultHttpClient();
final HttpGet request = new HttpGet(
"http://www.mysitehere.com"); // And yes, my url works and is not timing out from browser
request.addHeader(
"User-Agent",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36");
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
HttpResponse response = httpclient.execute(request); // This code times out.
Any help is greatly appreciated, thanks!
Edit: Example ip and port:
"101.96.11.10":"80"
You can specify proxy for each request using DynamicProxyRoutePlanner
:
PoolingHttpClientConnectionManager http
...
this.http = new PoolingHttpClientConnectionManager();
this.http.setMaxTotal(10);
this.http.setDefaultMaxPerRoute(10);
this.http.setValidateAfterInactivity(10000);
...
HttpHost proxy = new HttpHost(ADDRESS, PORT);
DynamicProxyRoutePlanner routePlanner = new DynamicProxyRoutePlanner(proxy);
HttpClients.custom()
.setConnectionManager(http)
.setDefaultRequestConfig(getRequestConfig(timeout))
.setRoutePlanner(routePlanner)
.setConnectionManagerShared(true)
.build();
...
private static RequestConfig getRequestConfig(int timeout) {
return RequestConfig.copy( RequestConfig.DEFAULT )
.setConnectTimeout( timeout )
.setConnectionRequestTimeout( timeout )
.setSocketTimeout( timeout )
.setContentCompressionEnabled( true )
.setRedirectsEnabled( true )
.setMaxRedirects( 10 )
.build();
}
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.