Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Proxy on WebView for API 26

firstly I have read all the answers written about questions similar to my question. But none of the solutions helped me.

this is my Webview Code

mWebView.loadUrl(siteUrl);       
mWebView.getSettings().setUserAgentString(ua);      
mWebView.getSettings().setJavaScriptEnabled(true);      
mWebView.getSettings().setDomStorageEnabled(true);      
mWebView.setWebViewClient(new mWebViewClient());

i tried to use this class https://gist.github.com/madeye/2297083

And I used this code to set proxy

ProxySettings.setProxy(getApplicationContext(), "x.x.x.x", 8080);

I also tested this class https://gist.github.com/WanghongLin/50032a4d3933960454a7

and

WebViewHttpProxy.setProxy(mWebView,"x.x.x.x",8080);

But none of them worked. and proxy was not applied when i ran the app

like image 526
Fardin Avatar asked Dec 20 '25 09:12

Fardin


1 Answers

Very easy set proxy on your android webview app ✅

compile 'androidx.webkit:webkit:1.3.0' setProxy(); just load before your webview Url loading .

private void init() {
    wv = findViewById(R.id.wv);
    WebSettings webSettings = wv.getSettings();
    webSettings.setSupportZoom(true);
    webSettings.setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm){
            
            handler.proceed("userName", "password");
        }
    });
    setProxy();
    wv.loadUrl("https://www.google.com");
}

private void setProxy() {
        ProxyConfig proxyConfig = new ProxyConfig.Builder()
                .addProxyRule("123.123.123.123:1234")
                .addDirect().build();
        ProxyController.getInstance().setProxyOverride(proxyConfig, new Executor() {
            @Override
            public void execute(Runnable command) {
                //do nothing
            }
        }, new Runnable() {
            @Override
            public void run() {
                
            }
        });
    }

Enjoy

like image 130
Coco Avatar answered Dec 21 '25 23:12

Coco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!