在WebView后面使用代理

时间:2013-05-14 04:59:46

标签: java android webview proxy webkit

它已经是一个星期,我搜索使webview与代理(不使用wifi,只是移动ddata)工作的方式。我还没找到任何解决方案。有人可以给我一些与它合作的东西吗?

我已经尝试了SO中的所有内容(可能不是全部,如果有人有办法请分享)

看看我的代码

 public class WebViewActivity extends Activity {

    static WebView web;
 String PROXY_IP = "202.58.124.34";  
 int PROXY_PORT = 8989; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view);

        web = (WebView) findViewById(R.id.webView1);
        web.setWebViewClient(new MyWebViewClient("username","password"));

        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        web.getSettings().setAllowContentAccess(true);

        web.getSettings().setAppCacheEnabled(true);

        System.getProperties().put("proxySet", "true"); 
        System.getProperties().put(PROXY_IP, "202.58.124.34");
        System.getProperties().put(PROXY_PORT, "8989");

        Authenticator authenticator = new Authenticator() { 

            public PasswordAuthentication getPasswordAuthentication() { 
            return (new PasswordAuthentication 
                    ("username","password".toCharArray())); 
            } 
    }; 
    Authenticator.setDefault(authenticator); 

        web.setHttpAuthUsernamePassword("http://202.58.124.34", "", "username", "password"); 

        web.loadUrl("URL");  


    }

    public static boolean setProxyICSPlus(WebView webview, String host, int port, String exclusionList) {

         Log.d("", "Setting proxy with >= 4.1 API.");

         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 mWebViewCoreFieldIntance = getFieldValueSafely(mWebViewCoreField, web);

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

           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("202.58.124.34", 8989, null)); 

    } catch (Exception ex) {
        Log.e("","Setting proxy with >= 4.1 API failed with error: " + ex.getMessage());
        return false;
    }

    Log.d("", "Setting proxy with >= 4.1 API successful!");
    return true;

    }

    private static Object getFieldValueSafely(Field field, Object classInstance) throws IllegalArgumentException, IllegalAccessException {
        boolean oldAccessibleValue = field.isAccessible();
        field.setAccessible(true);
        Object result = field.get(classInstance);
        field.setAccessible(oldAccessibleValue);
        return result;      
    }

    public void loadUrl(WebView view, String url, String proxyUserName, String proxyPassword){
        UsernamePasswordCredentials creds= new UsernamePasswordCredentials("username", "password");
        Header credHeader = BasicScheme.authenticate(creds, "UTF-8", true);
        Map<String, String> header = new HashMap<String, String>();
        header.put(credHeader.getName(), credHeader.getValue());
        view.loadUrl(url, header);
}

我已经尝试使用公共布尔ICS,使用try和catch方法。所有都没有显示任何结果(或者我做错了?)。所以,如果有人使用webview中的代理工作方式来表明它。

感谢。

注意:我正在使用移动数据运行。不是Wifi。所以设置只适用于移动数据。

0 个答案:

没有答案