1

我已经一个星期在寻找使 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 中使用代理来显示它。

谢谢。

注意:我正在使用移动数据运行。不是无线网络。因此该设置仅适用于移动数据。

4

0 回答 0