拦截并覆盖来自WebView的HTTP请求

编程入门 行业动态 更新时间:2024-10-18 14:24:59
本文介绍了拦截并覆盖来自WebView的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用程序中有一个WebView,其中打开了一些网站(总是一样,这是我自己的页面).该网站有一些JS代码,可从远程主机加载一些图像.

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.

有可能这样做吗?

编辑:问题的当前状态...

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(")不会触发此方法).它还可以通过返回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,但是无法取消它.这也使该方法也不适合我的任务.

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().

更多推荐

拦截并覆盖来自WebView的HTTP请求

本文发布于:2023-11-28 09:57:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WebView   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!