温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

安卓webview怎么拦截请求

发布时间:2026-09-20 06:07:03 来源:亿速云 阅读:141 作者:小樊 栏目:移动开发

在 Android WebView 中“拦截请求”通常有几种不同层级的做法,取决于你想拦截什么、做什么处理。下面按常见场景系统说明。


一、拦截 URL 跳转(最常用)

1. 使用 WebViewClient.shouldOverrideUrlLoading

适合:拦截点击链接、页面跳转、scheme(如 app://)

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("myapp://")) {
            // 拦截自定义协议
            handleCustomScheme(url);
            return true; // 不交给 WebView 处理
        }
        return false; // 继续加载
    }
});

✅ 特点:

  • 只能拦截“主资源请求”(页面级)
  • 不能拦截图片、JS、CSS 等子资源

二、拦截所有资源请求(API 21+)

2. 使用 WebResourceRequest(API 21+)

webView.setWebViewClient(new WebViewClient() {
    @Override
    public WebResourceResponse shouldInterceptRequest(
            WebView view, WebResourceRequest request) {

        String url = request.getUrl().toString();

        if (url.contains("block.com")) {
            return new WebResourceResponse(
                "text/plain",
                "utf-8",
                null // 返回空内容,相当于拦截
            );
        }
        return null; // 不拦截
    }
});

✅ 特点:

  • 可拦截:HTML / JS / CSS / 图片 / XHR
  • 不能修改请求头
  • 返回 null 表示继续

三、拦截并“替换响应内容”

3. 返回本地资源(缓存 / 本地 HTML)

@Override
public WebResourceResponse shouldInterceptRequest(
        WebView view, WebResourceRequest request) {

    if (request.getUrl().toString().endsWith("logo.png")) {
        try {
            InputStream is = getAssets().open("local_logo.png");
            return new WebResourceResponse(
                    "image/png",
                    "utf-8",
                    is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

常见用途:

  • 脱敏
  • 本地化资源
  • 离线包

四、拦截请求并“修改请求”(高级)

⚠️ WebView 本身不支持修改请求头

方案 1:拦截 → 自己用 HttpURLConnection 发请求

@Override
public WebResourceResponse shouldInterceptRequest(
        WebView view, WebResourceRequest request) {

    try {
        URL url = new URL(request.getUrl().toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Custom-Header", "value");

        return new WebResourceResponse(
                "text/html",
                "utf-8",
                conn.getInputStream());
    } catch (Exception e) {
        return null;
    }
}

方案 2:使用 shouldInterceptRequest(WebView, String)(已废弃)


五、拦截 JavaScript 请求(XHR / Fetch)

✅ 仍走 shouldInterceptRequest

如果你要:

  • 修改 XHR 请求头 ❌ WebView 不支持
  • 拦截 XHR URL ✅ 可以

六、Android 21 以下兼容

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    // 旧 API
    return null;
}

七、常见问题

Q1:为什么拦截不到图片?

  • 必须用 shouldInterceptRequest
  • 确保 WebView 已启用网络

Q2:能拦截 POST 请求吗?

  • ✅ 能拦截
  • ❌ 不能修改 POST body

Q3:能完全代理 WebView 吗?

  • 不能 100%
  • 推荐用 OkHttp 拦截 + 注入 JS

八、推荐方案总结

需求 推荐方式
拦截跳转 shouldOverrideUrlLoading
拦截资源 shouldInterceptRequest
修改响应 返回 WebResourceResponse
修改请求 自己发 HTTP 请求
完整代理 WebView + 本地 Proxy

如果你有具体场景(比如:拦截广告 / 修改 Header / 登录鉴权 / 离线包 / Hybrid),可以告诉我,我可以给你针对性代码。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手