[FIXED] How to set proxy for Android in a Xamarin C# application using WebView?

Issue

I have an Android application created in C# using Xamarin.

This is essentially a web browser with some additional functionalities and now I would like add the option to set up a proxy to use. In the application I use WebView for connection to websites.

I tried to implement solution from this topic (How can I set ProxySettings and ProxyProperties on Android Wi-Fi connection using Java?), but there is no linkProperties in WifiConfiguration. This is how it looks like:

private static Java.Lang.Object getField(Java.Lang.Object obj, String name)
{
   Field f = obj.Class.GetField(name);
   Java.Lang.Object o = f.Get(obj);
   return o;
}

public void SetHttpProxy(string proxyServerAddress, int proxyServerInt)
{
   try
   {
       var wifiManager = context.GetSystemService(Context.WifiService) as WifiManager;
       if (!wifiManager.IsWifiEnabled)
           return;

       var configurationList = wifiManager.ConfiguredNetworks;

       var cur = wifiManager.ConnectionInfo.NetworkId;
       var configuration = configurationList.FirstOrDefault(conf => conf.NetworkId == cur);

       getField(configuration, "linkProperties");

   }
   catch (Exception e)
   {
       throw;
   }
}

Solution

Here is solution that works for 4., 5., 6.* and 7.* Android OS versions for sure:

public static void SetProxy(WebView webView, string host, int port, bool bypass)
{
    Context appContext = webView.Context.ApplicationContext;
    JavaSystem.SetProperty("http.proxyHost", host);
    JavaSystem.SetProperty("http.proxyPort", port + "");
    JavaSystem.SetProperty("https.proxyHost", host);
    JavaSystem.SetProperty("https.proxyPort", port + "");
    if (bypass)
        JavaSystem.SetProperty("http.nonProxyHosts", BYPASS_PATTERN);

    try
    {
        Class applictionCls = Class.ForName(APPLICATION_CLASS_NAME);
        Field loadedApkField = applictionCls.GetField("mLoadedApk");
        loadedApkField.Accessible = true;
        Object loadedApk = loadedApkField.Get(appContext);
        Class loadedApkCls = Class.ForName("android.app.LoadedApk");
        Field receiversField = loadedApkCls.GetDeclaredField("mReceivers");
        receiversField.Accessible = true;
        ArrayMap receivers = (ArrayMap) receiversField.Get(loadedApk);
        foreach (Object receiverMap in receivers.Values())
        {
            foreach (Object rec in Extensions.JavaCast<ArrayMap>(receiverMap).KeySet())
            {
                Class clazz = rec.Class;
                if (clazz.Name.Contains("ProxyChangeListener"))
                {
                    Method onReceiveMethod = clazz.GetDeclaredMethod("onReceive", Class.FromType(typeof(Context)), Class.FromType(typeof(Intent)));
                    Intent intent = new Intent(Android.Net.Proxy.ProxyChangeAction);

                    onReceiveMethod.Invoke(rec, appContext, intent);
                }
            }
        }
    }
    catch (Exception)
    {
    }
}

Answered By – DeSubstantiisSeparatis

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published