Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpClient Doesn't Use System Proxy Settings

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?

like image 338
Cristian Avatar asked Dec 21 '10 20:12

Cristian


People also ask

Do android proxy settings apply to all apps on the device?

No, they do not apply globally and without root there is no way to force a proxy to be used by all applications. The reason the message you found is worded that way is that it is up to the app creator to respect the proxy settings and use them or do the wrong thing and ignore them.

What is proxy in HTTP client?

What Does HTTP Proxy Mean? An HTTP Proxy serves two intermediary roles as an HTTP Client and an HTTP Server for security, management, and caching functionality. The HTTP Proxy routes HTTP Client requests from a Web browser to the Internet, while supporting the caching of Internet data.

What is HTTP proxy in Android Studio?

Proxies serve as intermediary connection points between HTTP clients and web servers that add security and privacy to internet connections. To support running Android Studio behind a firewall, set the proxy settings for the Android Studio IDE.


2 Answers

Try:

DefaultHttpClient httpclient = new DefaultHttpClient();

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

(culled from here)

like image 70
CommonsWare Avatar answered Oct 02 '22 17:10

CommonsWare


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!

like image 42
Sakib I. Avatar answered Oct 02 '22 19:10

Sakib I.