- Java, Commons HTTP Client and HTTP proxies (Aaron Johnson). Check this the first example if you just need to set proxy name/host.
- 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:
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!
Latest HTTPClient :
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost proxy = new HttpHost("127.0.0.1", 8089);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
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()));
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
if(useProxyAuthentication){
Credentials credentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
AuthScope authScope = new AuthScope(proxyHost, proxyPort);
client.getState().setProxyCredentials(authScope, credentials);
}
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
Post a Comment