Apache HttpClient 4.1 - 代理设置

发布于 2024-10-16 22:05:33 字数 740 浏览 2 评论 0原文

我正在尝试将一些参数发布到服务器,但我需要设置代理。你能帮我对我的代码的“设置代理”部分进行排序吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

原谅我要高飞 2024-10-23 22:05:33

是的,我解决了我自己的问题,这一行

httpclient.getParams().setParameter("3128",proxy);

应该是

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

Apache HttpClient 4.1的完整示例,设置代理可以在下面找到

HttpHost proxy = new HttpHost("ip address",port number);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param name", param));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
HttpResponse response = httpclient.execute(httpost);

HttpEntity entity = response.getEntity();
System.out.println("Request Handled?: " + response.getStatusLine());
InputStream in = entity.getContent();
httpclient.getConnectionManager().shutdown();

Yes I sorted out my own problem,this line

httpclient.getParams().setParameter("3128",proxy);

should be

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

Complete Example of a Apache HttpClient 4.1, setting proxy can be found below

HttpHost proxy = new HttpHost("ip address",port number);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param name", param));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
HttpResponse response = httpclient.execute(httpost);

HttpEntity entity = response.getEntity();
System.out.println("Request Handled?: " + response.getStatusLine());
InputStream in = entity.getContent();
httpclient.getConnectionManager().shutdown();
聊慰 2024-10-23 22:05:33

不推荐使用的方法(也在 4.5.5 版本中)是:

HttpHost proxy = new HttpHost("proxy.com", 80, "http");
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
                    .setRoutePlanner(routePlanner)
                    .build();

Non deprecated way of doing it (also in 4.5.5 version) is:

HttpHost proxy = new HttpHost("proxy.com", 80, "http");
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
                    .setRoutePlanner(routePlanner)
                    .build();
野稚 2024-10-23 22:05:33

这是我用来设置代理的快速方法:

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;    
...
HttpHost proxy = new HttpHost("www.proxy.com", 8080, "http");
HttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build();

This is quick way I use to set the proxy:

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;    
...
HttpHost proxy = new HttpHost("www.proxy.com", 8080, "http");
HttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build();
污味仙女 2024-10-23 22:05:33

当我使用 apache httpclient v4.5.5 时,我发现 HttpClient.getParams() 在 v4.3 中已被弃用,我们应该使用 org.apache.http.client.config.RequestConfig 代替。
代码示例
表明:

 HttpHost target = new HttpHost("httpbin.org", 443, "https");
 HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");

 RequestConfig config = RequestConfig.custom()
     .setProxy(proxy)
     .build();
 HttpGet request = new HttpGet("/");
 request.setConfig(config);
 CloseableHttpResponse response = httpclient.execute(target, request);

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:

 HttpHost target = new HttpHost("httpbin.org", 443, "https");
 HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");

 RequestConfig config = RequestConfig.custom()
     .setProxy(proxy)
     .build();
 HttpGet request = new HttpGet("/");
 request.setConfig(config);
 CloseableHttpResponse response = httpclient.execute(target, request);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文