Apache HttpClient 4.1 - Proxy Settings
Asked Answered
S

4

34

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();
Shellyshelman answered 10/2, 2011 at 10:4 Comment(0)
S
68

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();
Shellyshelman answered 10/2, 2011 at 12:34 Comment(2)
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); is deprecatedBaram
HttpHost proxy = new HttpHost("ip address",port number); DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); Works like a charm on 4.5.6Joyann
B
60

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();
Baram answered 20/7, 2015 at 16:15 Comment(3)
You should use new HttpHost("proxy.com", 80, HttpHost.DEFAULT_SCHEME_NAME) or new HttpHost("proxy.com", 80)Guillemette
Thank you very much, I was a little bit confused with apache commons http vs httpcomponents, now I can you both of themLenee
Thank you for the non deprecated solution :)Epoch
T
9

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();
Timmie answered 25/7, 2018 at 13:13 Comment(0)
S
6

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);
Scrim answered 27/3, 2018 at 1:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.