拦截并忽略的WebView HTTP的请求WebView、HTTP

2023-09-12 10:45:13 作者:请别拿我的感情开玩笑

我在我的应用程序的WebView其中一些网站开通(总是相同的,这是我自己的页面)。该网站有一些JS code,加载一些图像从远程主机。

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.

我想拦截请求这些影像(URL模式),并给回我自己的内容(也就是另一个图像),或依赖于内部应用程序逻辑的请假申请不变。

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 已设置能力的 WebViewClient (所指出的Axarydax)。 WebViewClient 有两个有用的方法

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

shouldOverrideUrlLoading onLoadResource shouldOverrideUrlLoading onLoadResource

shouldOverrideUrlLoading 能够拦截任何URL负载,负载是通过页面交互触发(即页面上链接被点击,WebView.loadURL()ISN'牛逼触发此方法)。它还可以通过返回false就取消URL负载。这种方法不可用,原因是不能够拦截加载页面资源(和图像,我需要拦截,这样的页面资源)。

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 被触发,每次该网页资源(和图像!THX到jessyjones)正在加载,但没有办法取消。这使得这种方法不适合我的任务也。

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.

推荐答案

它看起来像API级别11具有你所需要的支持。请参阅WebViewClient.shouldInterceptRequest().

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

 
精彩推荐
图片推荐