简体   繁体   English

在InAppBrowser中使用代理服务器-PhoneGap

[英]Using proxy server in InAppBrowser - PhoneGap

I'm building a phonegap application that uses one of my servers as a proxy. 我正在构建一个将我的服务器之一用作代理的phonegap应用程序。 Unfortunately, there is no official API or documentation to do so. 不幸的是,没有官方的API或文档。 So, my question is how can I use a proxy with InAppBrowser. 因此,我的问题是如何在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. 注意:此快照来自于官方的phonegap文档。

Regards, Dawoud 此致Dawoud

You can change the inappbrowser.java code before loadUrl in run function like this: 您可以在run函数中的loadUrl之前更改inappbrowser.java代码,如下所示:

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
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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