A background.

Many friends will encounter the company’s APP needs to access the company’s Intranet service through the extranet. At this time, the background classmate will configure a proxy server, and the APP will access the company’s Intranet through the proxy server. Requests to access the proxy server are also authenticated for security reasons.

So how to set the proxy of Android WebView? I checked the information on the Internet before, most of them are set through reflection, but Google has provided a convenient API for us to use. So, here is a brief introduction to the proxy Settings of the WebView.

II. Specific steps

Android WebView set agent needs to use ProxyConfig related classes, in WebViewClient onReceivedHttpAuthRequest authentication required. First, we add dependencies to the project’s build.gradle.

Implementation 'androidx. Its: its: 1.3.0'

The specific code for setting agent is as follows:

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, HttpAuthHttpAuthHandler handler, String Host, String Realm){// authenticate (password) handler. Proceed ("userName", "password"); }}); setProxy(); wv.loadUrl("http://www.uc123.com"); } private void setProxy() { if (WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE)) { ProxyConfig ProxyConfig = new ProxyConfig.Builder().addProxyRule("111.123.321.121:1234").addDirect().build(); ProxyController.getInstance().setProxyOverride(proxyConfig, new Executor() { @Override public void execute(Runnable command) { //do nothing } }, New Runnable() {@Override public void run() {log.w (TAG, "WebView proxy change "); }}); }}