Saturday, April 12, 2008

How to use org.apache.commons.httpclient and excute method via proxy

I try to find out how to use httpclient with proxy and do not want to change java system property. But it seems that we can not find much document about it.
Finally, i have find out the solution:

First: Create httpclient
HttpClient client = new HttpClient();

set proxy
client.getHostConfiguration().setProxy(proxyhost, proxyPort);

//create authenticate
AuthScope auth = new AuthScope(proxyhost,proxyPort, AuthScope.ANY_REALM);
Credentials cred = new UsernamePasswordCredentials("","");
client.getState().setProxyCredentials(auth, cred);
=> now we can connect to internet through proxy.

If you want more information, please post comment. I will answer.

Google