Apache HttpClient 4.1 - 代理设置
我正在尝试将一些参数发布到服务器,但我需要设置代理。你能帮我对我的代码的“设置代理”部分进行排序吗?
HttpHost proxy = new HttpHost("xx.x.x.xx");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter("3128",proxy);
HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("aranan", song));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
System.out.println("Request Handled?: " + response.getStatusLine());
in = entity.getContent();
httpclient.getConnectionManager().shutdown();
I am trying to POST some parameters to a server, but I need to set up the proxy. can you help me to to sort it "setting the proxy" part of my code ?
HttpHost proxy = new HttpHost("xx.x.x.xx");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter("3128",proxy);
HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("aranan", song));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
System.out.println("Request Handled?: " + response.getStatusLine());
in = entity.getContent();
httpclient.getConnectionManager().shutdown();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,我解决了我自己的问题,这一行
应该是
Apache HttpClient 4.1的完整示例,设置代理可以在下面找到
Yes I sorted out my own problem,this line
should be
Complete Example of a Apache HttpClient 4.1, setting proxy can be found below
不推荐使用的方法(也在 4.5.5 版本中)是:
Non deprecated way of doing it (also in 4.5.5 version) is:
这是我用来设置代理的快速方法:
This is quick way I use to set the proxy:
当我使用 apache httpclient v4.5.5 时,我发现 HttpClient.getParams() 在 v4.3 中已被弃用,我们应该使用 org.apache.http.client.config.RequestConfig 代替。
代码示例
表明:
When I use apache httpclient v4.5.5,I found HttpClient.getParams() is deprecated in v4.3,we should use
org.apache.http.client.config.RequestConfig
instead.Code sample
shows that: