Tuesday, March 18, 2008

Apache HTTP Client and proxy settings

I don't how many times I've had to do it (and how many times I've screwed it up), but I can never remember just how to set up proxies for Apache HTTP Client. So, to keep a record of my googling and experimenting, here's the entries that really helped:
  • jGuru - just the last thread comment (Vivek Singh - Aug 16, 2007). Check this out if you need to set authentication parameters, as well.

Essentially here's what I ended up with (if you just need proxy name and port):

HttpClient client = new HttpClient();
client.getHostConfiguration().setProxy(proxyHost, port);


6 comments:

Zack said...

Thanks a bunch.
I've already had to code this line a couple of times before, but I just couldn't figure it out on my own anymore...
Thanks for sharing this so little but so precious piece of global knowledge!

Raja Sekhar Chaliki said...

Latest HTTPClient :

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost proxy = new HttpHost("127.0.0.1", 8089);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

Raja Sekhar Chaliki said...

if u need basic authentication:

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost proxy = new HttpHost("127.0.0.1", 8089);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

List authpref = new ArrayList();
authpref.add(AuthPolicy.BASIC);
httpclient.getCredentialsProvider().setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(this.userID,
getPassword()));

. said...

client.getHostConfiguration().setProxy(proxyHost, proxyPort);
if(useProxyAuthentication){
Credentials credentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
AuthScope authScope = new AuthScope(proxyHost, proxyPort);
client.getState().setProxyCredentials(authScope, credentials);
}

Krystian Nowak said...
This comment has been removed by the author.
Krystian Nowak said...

in https://issues.apache.org/jira/browse/HTTPCLIENT-1128 SystemDefaultHttpClient was added to ver. 4.2 – see http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/SystemDefaultHttpClient.html