2

I have an app that makes http requests to a remote server. I do this with the following code:

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("myURL");

    try {

        ArrayList<BasicNameValuePair> postVariables = new ArrayList<BasicNameValuePair>(2);
        postVariables.add(new BasicNameValuePair("key","value"));

        httpPost.setEntity(new UrlEncodedFormEntity(postVariables));
        HttpResponse response = httpClient.execute(httpPost);
        String responseString = EntityUtils.toString(response.getEntity());

        if (responseString.contains("\"success\":true")){
            //this means the request succeeded
        } else {
            //failed
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

This goes really well, but one of our customers has set up an APN that requires requests to go via a certain proxy server. If I add the following to the request this works, the request gets rerouted via the proxy to the server:

    HttpHost httpHost = new HttpHost("proxyURL",8080);
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);

So far so good, however, I use a library that makes some http requests as well. The library's code is not accesible for me, so I can't add those two lines to the code. I contacted the creators of that library, and they told me it should be possible to set up the android environment so that all requests will automatically go through the proxy. Is there something like that? I didn't find anything on google.
I'm basically looking for a way to set the above two lines as a standard for all http requests. Please note that the APN does not set the proxy as a default for the entire phone, so apps will have to do this manually (and yes that means the majority of the apps don't work on that customer's phone).

Kevin
  • 2,739
  • 33
  • 57
  • You may not remember now but did you found the code which can redirect all device network traffic via a proxy? If possible can you please share the code? – sak Jul 27 '18 at 09:37
  • @sak Sorry, no idea if I even got this working. Have you tried David's answer? Keep in mind this was asked 5 years ago, Android has changed a lot since then. – Kevin Jul 27 '18 at 17:02
  • Yeah i know that but i am not getting any solution, trying from a long time, according to David i think its not possible to redirect all device traffic to a proxy because he mentioned that "each app runs in it's own Dalvik so you won't impact other app's network communications". But thanks at least for replying. – sak Jul 28 '18 at 07:12
  • @sak Good luck, I remember I originally asked this because I needed a way to control this via the app (locked down enterprise app on company phones that weren't managed at all, but had to connect through an APN to get the relevant data), but the phone itself has proxy settings that already allow this. Is that an option for you, just using the settings of the phone? – Kevin Jul 29 '18 at 01:47
  • Yes I can use the phone settings but it also doesn't work for all apps, I have tried that before. – sak Aug 06 '18 at 12:03
  • I have just got permission to use VPN, so can i redirect all data via VPN? – sak Aug 06 '18 at 12:07

1 Answers1

1


It's been a year or two since I've needed to use it, but if I remember correctly, you can use the System.setProperty(String, String) in order to set an environment-wide setting for your application to route all HTTP traffic through a proxy. The properties that you should need to set are "http.proxyHost" and "http.proxyPort" and then use your HttpClient normally without specifying a proxy because the VM will handle routing requests.
Docs for more information about what I'm talking about can be found here: ProxySelector (just so you know what keys to use) and here for documentation about the actual System.setProperty(String, String) function
If that doesn't work for you, let me know and I'll try to dig out my old code that set a system-level proxy. BTW, it's really only "system-level" since each app runs in it's own Dalvik so you won't impact other app's network communications.

David C. Sainte-Claire
  • 2,461
  • 1
  • 15
  • 12
  • Thanks, I will take a look at it, it would be indeed exactly what I need. I don't want to influence other apps :). – Kevin Jun 25 '13 at 07:33
  • I can not test this yet, but I read up on your suggestions and I think this is the right way. Will mark as answered, thank you. – Kevin Jun 26 '13 at 09:04
  • Did anyone get this working? Still cannot get it to work. tried http.ProxyHost http.ProxyPort https.ProxyHost and https.ProxyPort. Can't get it to work. Can any one provide a sample showing the values used? – rideintothesun Oct 10 '18 at 12:05