代码之家  ›  专栏  ›  技术社区  ›  ZekeMarsh

如何使用WebView在Xamarin C#应用程序中为Android设置代理?

  •  2
  • ZekeMarsh  · 技术社区  · 6 年前

    我使用Xamarin在C#中创建了一个Android应用程序。

    这本质上是一个具有一些附加功能的web浏览器,现在我想添加一个选项来设置要使用的代理。在应用程序中,我使用WebView连接到网站。

    我试图实现此主题的解决方案( How can I set ProxySettings and ProxyProperties on Android Wi-Fi connection using Java? ),但WifiConfiguration中没有linkProperties。这就是它的样子:

    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;
       }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   DeSubstantiisSeparatis    6 年前

    以下是适用于4的解决方案。 5. ,6。*和7。*Android操作系统版本:

    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)
        {
        }
    }