How to reset proxy in Android WebView for Android

2019-03-02 17:48发布

I am using following 2 methods to set proxy in my Android WebView for Android versions ICS and JB. But I am not able to reset/ remove this proxy for these 2 versions. How proxy being set by these methods can be resetted/ removed?

For ICS:

private static boolean setProxyICS(WebView webview, String host, int port) {
        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 mWebViewCoreFieldInstance = getFieldValueSafely(
                    mWebViewCoreField, webview);

            Class wvc = Class.forName("android.webkit.WebViewCore");

            Field mBrowserFrameField = wvc.getDeclaredField("mBrowserFrame");

            Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField,
                    mWebViewCoreFieldInstance);

            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(host, port, null));

        } catch (Exception e) {
            return false;
        }
        return true;
    }

and for JellyBean:

private static boolean setProxyJB(WebView webview, String host, int port) {
        try {
            Class wvcClass = Class.forName("android.webkit.WebViewClassic");
            Class wvParams[] = new Class[1];
            wvParams[0] = Class.forName("android.webkit.WebView");
            Method fromWebView = wvcClass.getDeclaredMethod("fromWebView",
                    wvParams);
            Object webViewClassic = fromWebView.invoke(null, webview);

            Class wv = Class.forName("android.webkit.WebViewClassic");
            Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
            Object mWebViewCoreFieldInstance = getFieldValueSafely(
                    mWebViewCoreField, webViewClassic);

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

            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);

            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);

            updateProxyInstance.invoke(sJavaBridge, ppcont.newInstance(host, port, null));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

0条回答
登录 后发表回答