本文主要探讨了在Android平台上如何拦截并获取WebView内部的POST请求参数,提供了两种具体的实现方案。这两种方法对于那些希望在H5页面不知情的情况下获取其请求参数的开发者来说尤其有用,特别是在处理非自身控制的H5页面时。 ### 方案一:直接拦截所有请求 最初的尝试是通过重写`WebViewClient`的`shouldInterceptRequest`方法来实现。这种方法可以捕获到WebView加载的所有URL请求,但有一个限制,即它只能获取GET请求的参数,因为GET参数直接包含在URL中。代码示例如下: ```java webView.setWebViewClient(new WebViewClient() { @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { try { URL url = new URL(request.getUrl()); Log.e("InternetActivity", request + ""); } catch (MalformedURLException e) { e.printStackTrace(); } return super.shouldInterceptRequest(view, request); } }); ``` 然而,由于POST请求的参数不在URL中,所以这种方法无法获取POST请求的数据。 ### 方案二:利用JavaScript接口 第二种方案是通过注入JavaScript代码到WebView中,监听H5页面中的Ajax请求,并在每次请求时调用Android原生的方法,将请求参数传递给客户端。这一方法的关键在于创建一个JavaScript接口,使得JavaScript能够与Android原生代码通信。以下是JavaScript注入的示例代码: ```javascript <script language="JavaScript"> function generateRandom() { return Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); } // This only works if `open` and `send` are called in an asynchronous way // That is, after calling `open`, there must be no other call to `open` or // `send` var xhr = new XMLHttpRequest(); xhr.open('POST', 'your-url-here'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { // Call Android native method with the request parameters window.android.onAjaxComplete(xhr.responseText); } }; xhr.send('key1=value1&key2=value2'); </script> ``` 在这个例子中,当H5页面发起POST请求时,JavaScript会在`onreadystatechange`回调中调用`window.android.onAjaxComplete`,将请求的响应数据传递给Android。为了实现这个接口,你需要在Android代码中注册一个JavaScriptInterface: ```java webView.addJavascriptInterface(new JavaScriptInterface(this), "android"); public class JavaScriptInterface { private Context context; public JavaScriptInterface(Context context) { this.context = context; } @JavascriptInterface @RequiresApi(api = Build.VERSION_CODES.KITKAT) public void onAjaxComplete(String responseData) { // 在这里处理接收到的POST请求参数 Log.e("InternetActivity", "Received AJAX response: " + responseData); } } ``` 通过这种方式,Android应用可以捕获并处理H5页面中的POST请求参数,即使这些请求是由JavaScript异步执行的。 总结,虽然直接拦截请求的方法无法获取POST参数,但结合JavaScript接口,我们可以成功地从WebView内部的POST请求中提取所需信息。这种技术对于需要监控或操作H5页面网络请求的Android开发者来说是非常有用的。
![](https://csdnimg.cn/release/download_crawler_static/12749779/bg1.jpg)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 2
- 资源: 954
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
会员权益专享
最新资源
- 利用迪杰斯特拉算法的全国交通咨询系统设计与实现
- 全国交通咨询系统C++实现源码解析
- DFT与FFT应用:信号频谱分析实验
- MATLAB图论算法实现:最小费用最大流
- MATLAB常用命令完全指南
- 共创智慧灯杆数据运营公司——抢占5G市场
- 中山农情统计分析系统项目实施与管理策略
- XX省中小学智慧校园建设实施方案
- 中山农情统计分析系统项目实施方案
- MATLAB函数详解:从Text到Size的实用指南
- 考虑速度与加速度限制的工业机器人轨迹规划与实时补偿算法
- Matlab进行统计回归分析:从单因素到双因素方差分析
- 智慧灯杆数据运营公司策划书:抢占5G市场,打造智慧城市新载体
- Photoshop基础与色彩知识:信息时代的PS认证考试全攻略
- Photoshop技能测试:核心概念与操作
- Photoshop试题与答案详解
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)