Intercept and override HTTP requests from WebView

2019-01-12 15:55发布

I have a WebView in my application in which some site is opened (always the same, it is my own page). The site has some JS code which loads some images from the remote host.

I want to intercept requests to such images (by URL pattern) and give back my own content (i.e. another image), or leave request untouched depending on internal application logic.

Is it possible to do that?

EDIT: The current state of question...

WebView has an ability to set a WebViewClient (as noted by Axarydax). WebViewClient have two useful methods

  • shouldOverrideUrlLoading
  • onLoadResource

shouldOverrideUrlLoading is able to intercept any url loading, if loading is triggered by page interaction (i.e. link on a page is clicked, WebView.loadURL("") isn't triggering this method). It is also able to cancel URL loading by returning false. This approach isn't usable, cause' it is not able to intercept loading of page resources (and images, what i need to intercept, is such a page resource).

onLoadResource is triggering every time that page resource (and images! thx to jessyjones) are loading, but there is no way to cancel that. That makes this method not suitable for my task also.

7条回答
你好瞎i
2楼-- · 2019-01-12 16:34

An ultimate solution would be to embed a simple http server listening on your 'secret' port on loopback. Then you can substitute the matched image src URL with something like http://localhost:123/.../mypic.jpg

查看更多
Explosion°爆炸
3楼-- · 2019-01-12 16:35

It looks like API level 11 has support for what you need. See WebViewClient.shouldInterceptRequest().

查看更多
老娘就宠你
4楼-- · 2019-01-12 16:40

You don't mention the API version, but since API 11 there's the method WebViewClient.shouldInterceptRequest

Maybe this could help?

查看更多
▲ chillily
5楼-- · 2019-01-12 16:48

As far as I know, shouldOverrideUrlLoading is not called for images but rather for hyperlinks... I think the appropriate method is

@Override public void onLoadResource(WebView view, String url)

This method is called for every resource (image, styleesheet, script) that's loaded by the webview, but since it's a void, I haven't found a way to change that url and replace it so that it loads a local resource ...

查看更多
淡お忘
6楼-- · 2019-01-12 16:54

Try this, I've used it in a personal wiki-like app:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("foo://")) {
            // magic
            return true;
        }
        return false;
    }
});
查看更多
Luminary・发光体
7楼-- · 2019-01-12 16:57

This may helps:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    String url = request.getUrl().toString();
    WebResourceResponse response = super.shouldInterceptRequest(view, request);
    // load native js
    if (url != null && url.contains(INJECTION_TOKEN/* scheme define */)) {

        response = new WebResourceResponse(
                "text/javascript",
                "utf-8",
                loadJsInputStream(url, JsCache.getJsFilePath(path) /* InputStream */));
    }
    return response;
}
查看更多
登录 后发表回答