代理在HttpClient 4.0 beta2中不起作用

时间:2009-05-17 08:21:39

标签: proxy httpclient

我正在使用HttpClient 4.0-beta2来进行REST调用。它在我的笔记本电脑上工作正常,但在uni中,我们必须配置我们的应用程序以通过代理,否则我们无法连接到互联网

这是我的原始代码:

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()));

以下是我使用代理的方式,但它对我不起作用。

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()));

1 个答案:

答案 0 :(得分:0)

尝试使用

ConnRouteParams.setDefaultProxy(method.getParams(), new HttpHost("yourproxyname",yourport,"http"));