Let’s say something about android webview, when we open webpage in an android application, the http header would some how add a field named x-requested-with to show your app’s package name. For some websites may prohibit any further access according to this field and their webpages are only available for their own app and browsers. To tackle this problem and make our app to access these webpages we need to modify webview client.
Analysis
From Android API 11 (3.0),WebView supply a new API in WebViewClient, like below:
1 | public WebResourceResponse shouldInterceptRequest(WebView view, String url) |
we can use this by call setWebViewClient.
but, in API21, a change occurs in this function,
1 | public WebResourceResponse shouldInterceptRequest(WebView view, final WebResourceRequest request) |
Implement
In order to adapt all the devices, both function should be implemented.
1 | webView.setWebViewClient(new WebViewClient() { |
For our demand, we need to change all the url which requested and add a param as a plus to mark.
1 | public static String injectIsParams(String url) { |
Attention: to do different operation by identify http and https.
More
- construct WebResourceResponse a intercept the http request and use local image to change the image from web.
1 | WebResourceResponse response = null; |
for the version above API 21 (5.0), use WebResourceRequest interface to modify header
1
2
3
4
public Map<String, String> getRequestHeaders() {
return request.getRequestHeaders();
}distinguish the different request from Get to Post by use this function and could be used to identify some differences about AJAX .
Problems
the webpage which I tended to modify actually did not rendered by the android browser, they are just some html raw characters, which is not a successfully experiment apparently, and I will keep looking for the reasons and if success I’ll refresh this blog.