13

When I create a DefaultHttpClient object and try to hit a webpage, the request isn't routed through the proxy I specified in Settings.

Looking through the API docs, I don't see anywhere where I can specify a proxy though Android does have a Proxy class that allows me to read the system's proxy settings.

Is there a way I can use the proxy settings in an HttpClient?

Cristian
  • 42,563
  • 25
  • 88
  • 99
  • There is a constructor for `HttpRoute` which takes a proxy host as a parameter to form a route .. but I don't see how to create a HttpClient that will use it. http://developer.android.com/reference/org/apache/http/conn/routing/HttpRoute.html – David J. Liszewski Dec 21 '10 at 20:34
  • Yeah, I saw that as well, but I couldn't figure out how to connect them together. – Cristian Dec 21 '10 at 20:53
  • 1
    Oooh .. did you see this answer ? http://stackoverflow.com/questions/2539669/android-unable-to-make-httprequest-behind-firewall/2542928#2542928 – David J. Liszewski Dec 21 '10 at 21:12

4 Answers4

24

Try:

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpHost proxy = new HttpHost("someproxy", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

(culled from here)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    perfekt, but I have to handle Webrequests on Devices, which may use (different) Proxys or no proxy settings, is there a possibility to figure out if the user had proxysettings? Some of my Clients are using different closed usergroups. – 2red13 Oct 11 '11 at 09:07
  • For getting the proper Proxy from the system check out this answer: http://stackoverflow.com/questions/10811698/getting-wifi-proxy-settings-in-android/13616054#13616054 – Adam Nov 29 '12 at 00:26
  • It will take too much time to connect to proxy after set params. In my app I have lots of https requests and each instance take long time to connect with proxy. Is there any better solution? – Crazy Coder May 13 '15 at 11:44
  • My god... this is awsome – Jocheved Jun 23 '15 at 05:17
  • 1
    @CommonsWare can u plz explain what will happen if the proxy server is down,i mean will we get any response codes defining that the proxy was successfully set or if not the case then can we know whether the proxy server is up and running... – Pratswinz Oct 19 '15 at 09:42
  • I asked the above question because i want to route my requests to main server directly if the proxy server is down – Pratswinz Oct 19 '15 at 09:43
9

Firstly, I would make sure that the request is adhering to the proxy settings properties you set in the Android Device's settings. You can determine this via code by looking at the System class in android.provider.Settings;

To identify if the user had system proxy settings, you can do the following:

    System.getProperty("http.proxyHost");
    System.getProperty("http.proxyPort");

    System.getProperty("https.proxyHost");
    System.getProperty("https.proxyPort");

If you have an instance of DefaultHTTPClient, then you can check whether it has the relevant proxy settings as well.

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY);

These are all ways to 'get' the proxy settings, and the 'set' methods are implemented in the same way, either through System.setProperty or httpclient.setParams.

Hope this helped!

Sakib I.
  • 91
  • 1
  • 2
3

I'm developing the Android Proxy Library that try to abstract the access to proxy settings for every Android version. You can easily get the proxy settings currently selected by the user.

lechuckcaptain
  • 1,032
  • 1
  • 9
  • 25
1

Try :

System.setProperty("http.proxyHost", <your proxy host name>);
System.setProperty("http.proxyPort", <your proxy port>);

or

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost httpproxy = new HttpHost("<your proxy host>",<your proxy port>);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,  httpproxy);

or

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