13

The Android developer docs say the following about Settings.Secure.HTTP_PROXY:

Host name and port for global http proxy. Uses ':' seperator for between host and port TODO - deprecate in favor of global_http_proxy_host, etc

But there is not any information about global_http_proxy_host. Now I always get null when trying to read Settings.Secure.HTTP_PROXY.

Settings.Secure.HTTP_PROXY was working properly in all releases before ICS.

Safecoder
  • 1,035
  • 2
  • 17
  • 32

2 Answers2

5

You can use Java Reflection to set the global proxy tested on ICS.

UPDATED CODE

Activity activity = this;


private void setProxtAndPortOnICS(String porxyServer2, int port2)
{
            try
{
  Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge");
  Class params[] = new Class[1];
  params[0] = Class.forName("android.net.ProxyProperties");
  Method updateProxyInstance = jwcjb.getDeclaredMethod("updateProxy", params);

  Class wv = Class.forName("android.webkit.WebView");
  Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
  Object mWebViewCoreFieldIntance = getFieldValueSafely(mWebViewCoreField, oauthPage);

  Class wvc = Class.forName("android.webkit.WebViewCore");
  Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");
  Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField, mWebViewCoreFieldIntance);

  Class bf = Class.forName("android.webkit.BrowserFrame");
  Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
  Object sJavaBridge = getFieldValueSafely(sJavaBridgeField, mBrowserFrame);

  Class ppclass = Class.forName("android.net.ProxyProperties");
 Class pparams[] = new Class[3];
 pparams[0] = String.class;
 pparams[1] = int.class;
 pparams[2] = String.class;
 Constructor ppcont = ppclass.getConstructor(pparams);

 updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance("my.proxy.com", 1234, null)); 
}
catch (Exception ex)
{    
 }


 }


 private Object getFieldValueSafely(Field field, Object classInstance) throws IllegalArgumentException, IllegalAccessException {
   boolean oldAccessibleValue = field.isAccessible();
   field.setAccessible(true);
   Object result = field.get(classInstance);
   field.setAccessible(oldAccessibleValue);
   return result;      
}

NOW you can filter out the urls using proxy server.

OR look at this blog this is in Chinese but you can read the code it is fairly easy to understand.

Sunny
  • 14,522
  • 15
  • 84
  • 129
  • Thanks, Sunny! Could you please let me know what is oauthPage used in your code example? So this will set proxy for your own app or it will impact all apps globally? – Safecoder May 24 '12 at 18:51
  • So if I understand it correctly, this only sets proxy for the single oauthPage,which is a webview object. Is it correct? Can I use it to set proxy for all my http traffic for my app? What I am looking for is a global setting, meaning it would affect other apps as well, for example the browser. Please let me know your thoughts. Thanks! – Safecoder May 24 '12 at 19:05
  • I tried a simple test case with just one Webview object. Then used your code to set proxy. Because you use my.proxy.com:1234 as proxy, so I would expect the webview would fail to load because such proxy does not exist. But it loads google.com without any problem. Do you have a working project that I can try? I also tried the code in your link and called ProxySettings.setProxy(this, "my.proxy.com", 1234); but also no effect. Webview load page just fine. – Safecoder May 24 '12 at 23:36
  • This will set the GLOBAL PROXY if you want to use it in your webview then you have to override a method in `webviewclient` which authenticate the username/password combo. Actually above code is not complete I'll post the complete code after some time.I am busy right now in office. – Sunny May 25 '12 at 05:55
  • Thank, Sunny. Looking forward to your update. Really appreciate it! – Safecoder May 25 '12 at 15:53
  • Thanks, Sunny! Do I need to add this code to all activities that has webview embedded? I tried a simple example with two activities. Main activity has a button says open google.com. Once it is clicked, it will bring up the second activity which has webview for google.com. I tried to add the code to the second activity and it worked. But if I only added the code to the first activity (remove it from the second), it does not work. That's what I meant by Global. Do I need to add it to all activities in my project? Thanks! – Safecoder May 25 '12 at 18:19
  • By the way, i did not override the onReceivedHttpAuthRequest method because I am just trying to load google.com and there is no authentication. – Safecoder May 25 '12 at 21:26
  • I never tried it in multiple webview but it set the Global proxy. means after setting it if you open any other browser then urls are filtered from the proxy server in that browser – Sunny May 26 '12 at 09:22
  • That's exactly what I want but still have problem get it working with the code you provided. So I made a simple app that has one button. Once clicked, it calls setProxtAndPortOnICS("foobar", 1234). So I did that, closed my app, then open Android browser. If the proxy was set successfully, all web access should fail because there is no foobar:1234 proxy running. But I can still go to google.com, yahoo.com without problems. I also tried a real proxy and from the log I see browser is not going through it. If you have a working project, could you please send to me howardli8888@gmail.com Thanks! – Safecoder May 26 '12 at 16:32
  • I will accept this answer as it is probably the best we can do. Even though not really global, but at least I got one webview working fine. Thanks, Sunny! – Safecoder Jul 11 '12 at 22:04
1

I'm just going by what the documentation says, but it reads to me that Settings.Secure.HTTP_PROXY isn't currently deprecated. The (sloppy) note in the documentation was just a developer's note that this is something the Android team may consider doing in the future.

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
  • Thanks, Tony. Just added more information to the original post. I am looking for a way to set global proxy in ICS. I have been using a hidden activity to set Settings.Secure.HTTP_PROXY for releases prior to ICS. But it is no longer working any more. Any suggestions? – Safecoder May 20 '12 at 01:18