Question

I am using HttpClient 4.0-beta2, to do REST calls. It works fine in my laptop, but in uni, we have to config our application to go through a proxy, otherwise we cannot connect to internet

Here is my orginal code:

HttpClient httpclient = new DefaultHttpClient();
HttpPut put = new HttpPut("http://" + PutBlob.ACCOUNT + 
                                      ".blob.core.windows.net/container/abc");
put.addHeader(PutBlob.ContentType, PutBlob.CONTENT_TYPE.TEXT_PLAIN.getValue());
put.setEntity(new StringEntity("Hello world", "UTF-8"));
Sign(put, PutBlob.ACCOUNT, PutBlob.KEY);

log.debug(EntityUtils.toString(httpclient.execute(put).getEntity()));

And below is how I use the proxy, but it didn't work for me.

What is the right way to config proxy in HttpClient 4.0 ?

HttpHost hcProxyHost = new HttpHost("proxyserver", 3128, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, hcProxyHost);

HttpPut put = new HttpPut("/container/abc");
put.addHeader(PutBlob.ContentType, PutBlob.CONTENT_TYPE.TEXT_PLAIN.getValue());
put.setEntity(new StringEntity("Hello world", "UTF-8"));
Sign(put, PutBlob.ACCOUNT, PutBlob.KEY);

HttpHost target = new HttpHost( PutBlob.ACCOUNT + ".blob.core.windows.net");
log.debug(EntityUtils.toString(httpclient.execute(target, put).getEntity()));
Was it helpful?

Solution

try to use

ConnRouteParams.setDefaultProxy(method.getParams(), new HttpHost("yourproxyname",yourport,"http"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top