简体   繁体   中英

Using proxy server in InAppBrowser - PhoneGap

I'm building a phonegap application that uses one of my servers as a proxy. Unfortunately, there is no official API or documentation to do so. So, my question is how can I use a proxy with InAppBrowser.

Here is the code I'm having right now:

var ref = window.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener('loadstart', function() { alert(event.url); });

NOTE: This snap is from official phonegap documentation.

Regards, Dawoud

You can change the inappbrowser.java code before loadUrl in run function like this:

Context appContext = cordova.getActivity().getApplicationContext();
System.setProperty("http.proxyHost", "yourhost");
System.setProperty("http.proxyPort", "yourport");
System.setProperty("https.proxyHost", "yourhost");
System.setProperty("https.proxyPort",  "yourport");
try {
Class applictionCls = Class.forName("android.app.Application");
            Field loadedApkField = applictionCls.getField("mLoadedApk");
            loadedApkField.setAccessible(true);
            Object loadedApk = loadedApkField.get(appContext);
            Class loadedApkCls = Class.forName("android.app.LoadedApk");
            Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
            receiversField.setAccessible(true);
            ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
            for (Object receiverMap : receivers.values()) {
                for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                    Class clazz = rec.getClass();
                    if (clazz.getName().contains("ProxyChangeListener")) {
                        Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                        Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

                        onReceiveMethod.invoke(rec, appContext, intent);
                    }
                }
            }
} catch (Exception e) {
    // TODO
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM