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?

3

4 Answers 4

24

Try:

DefaultHttpClient httpclient = new DefaultHttpClient();

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

(culled from here)

6
  • 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, 2011 at 9:07
  • For getting the proper Proxy from the system check out this answer: stackoverflow.com/questions/10811698/…
    – Adam
    Nov 29, 2012 at 0: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? May 13, 2015 at 11:44
  • My god... this is awsome
    – Jocheved
    Jun 23, 2015 at 5: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, 2015 at 9:42
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!

1
  • what happens if proxy settings give error or the proxy server is down,how to handle these situations
    – Pratswinz
    Oct 19, 2015 at 10:02
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.

0
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();
1
  • what happens if proxy settings give error or the proxy server is down,how to handle these situations
    – Pratswinz
    Oct 19, 2015 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.