Android HttpClient Doesn't Use System Proxy Settings

19,613

Solution 1

Try:

DefaultHttpClient httpclient = new DefaultHttpClient();

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

(culled from here)

Solution 2

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!

Solution 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.

Solution 4

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();
Share:
19,613

Related videos on Youtube

Cristian
Author by

Cristian

I'm a big fan of Python and Lisp dialects, but spend my day working in Java/Kotlin doing Android development

Updated on September 26, 2020

Comments

  • Cristian
    Cristian over 3 years

    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?

  • 2red13
    2red13 over 12 years
    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.
  • Adam
    Adam over 11 years
    For getting the proper Proxy from the system check out this answer: stackoverflow.com/questions/10811698/…
  • Crazy Coder
    Crazy Coder almost 9 years
    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?
  • Jocheved
    Jocheved almost 9 years
    My god... this is awsome
  • Pratswinz
    Pratswinz over 8 years
    @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
    Pratswinz over 8 years
    I asked the above question because i want to route my requests to main server directly if the proxy server is down
  • Pratswinz
    Pratswinz over 8 years
    what happens if proxy settings give error or the proxy server is down,how to handle these situations
  • Pratswinz
    Pratswinz over 8 years
    what happens if proxy settings give error or the proxy server is down,how to handle these situations

Related