tencent cloud

Feedback

Last updated: 2023-06-12 14:47:58
The Android system provides APIs to intercept network requests and inject custom logic in WebView. You can get the host of a URL request by intercepting various types of network requests from WebView, call HTTPDNS to resolve the host, and then request the network address by using the new URL formed with the obtained IP.
Note
Because the WebResourceRequest provided in shouldInterceptRequest(WebView view, WebResourceRequest request) does not contain request body information, the SDK can only successfully intercept GET requests, but not POST requests.
WebSettings webSettings = mWebView.getSettings();
// Adopt the default cache policy, i.e., the cache will always be used before it expires
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
// Load web image resources
webSettings.setBlockNetworkImage(false);
// Support JavaScript scripts
webSettings.setJavaScriptEnabled(true);
// Support zoom
webSettings.setSupportZoom(true);
mWebView.setWebViewClient(new WebViewClient() {
// Use this method for API 21 or later
@SuppressLint("NewApi")
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (request != null && request.getUrl() != null && request.getMethod().equalsIgnoreCase("get")) {
String scheme = request.getUrl().getScheme().trim();
String url = request.getUrl().toString();
Log.d(TAG, "url:" + url);
// HTTPDNS resolves the network and image requests of CSS files
if ((scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))
&& (url.contains(".css") || url.endsWith(".png") || url.endsWith(".jpg") || url .endsWith(".gif"))) {
try {
URL oldUrl = new URL(url);
URLConnection connection = oldUrl.openConnection();
// Get the HTTPDNS query result
String ips = MSDKDnsResolver.getInstance().getAddrByName(oldUrl.getHost());
String[] ipArr = ips.split(";");
if (2 == ipArr.length && !"0".equals(ipArr[0])) { // Get the IP successfully through HTTPDNS, replace the URL, and set the host header
String ip = ipArr[0];
String newUrl = url.replaceFirst(oldUrl.getHost(), ip);
connection = (HttpURLConnection) new URL(newUrl).openConnection(); // Set the host field of the HTTP request header
connection.setRequestProperty("Host", oldUrl.getHost());
}
Log.d(TAG, "contentType:" + connection.getContentType());
return new WebResourceResponse("text/css", "UTF-8", connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}

// Use this method for API 11–20
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (!TextUtils.isEmpty(url) && Uri.parse(url).getScheme() != null) {
String scheme = Uri.parse(url).getScheme().trim();
Log.d(TAG, "url:" + url);
// HTTPDNS resolves the network and image requests of CSS files
if ((scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))
&& (url.contains(".css") || url.endsWith(".png") || url.endsWith(".jpg") || url.endsWith(".gif"))) {
try {
URL oldUrl = new URL(url);
URLConnection connection = oldUrl.openConnection();
// Get the HTTPDNS query result
String ips = MSDKDnsResolver.getInstance().getAddrByName(oldUrl.getHost());
String[] ipArr = ips.split(";");
if (2 == ipArr.length && !"0".equals(ipArr[0])) { // Get the IP successfully through HTTPDNS, replace the URL, and set the host header
String ip = ipArr[0];
String newUrl = url.replaceFirst(oldUrl.getHost(), ip);
connection = (HttpURLConnection) new URL(newUrl).openConnection(); // Set the host field of the HTTP request header
connection.setRequestProperty("Host", oldUrl.getHost());
}
Log.d(TAG, "contentType:" + connection.getContentType());
return new WebResourceResponse("text/css", "UTF-8", connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
}
}
}
return null;
}});
// Load web resources
mWebView.loadUrl(targetUrl);

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support