ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android拦截并获取WebView内部POST请求参数

2021-12-10 15:31:55  阅读:261  来源: 互联网

标签:XMLHttpRequest 请求 url request send Android WebView prototype POST


Android拦截并获取WebView内部POST请求参数

实现过程

方案一

shouldInterceptRequest中拦截所有请求

webView.setWebViewClient(new WebViewClient() {
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        try {
            URL url = new URL(request.getUrl());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Log.e("InternetActivity", request + "");
        return super.shouldInterceptRequest(view, request);
    }

});

但是通过此方法只能获取get请求的参数(因为参数直接拼在了url链接中),对于post请求的参数无可奈何。

方案二

后来参考了request_data_webviewclient,有了新的实现方式,具体原理为:给H5注入一段js代码,目的是在每次Ajax请求都会调用Android原生的方法,将请求参数传给客户端。

js注入代码:如下:

<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 a synchronous way
    // That is, after calling `open`, there must be no other call to `open` or
    // `send` from another place of the code until the matching `send` is called.
    requestID = null;
    XMLHttpRequest.prototype.reallyOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        requestID = generateRandom()
        var signed_url = url + "AJAXINTERCEPT" + requestID;
        this.reallyOpen(method, signed_url , async, user, password);
    };
    XMLHttpRequest.prototype.reallySend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function(body) {
        interception.customAjax(requestID, body);
        this.reallySend(body);
    };

</script>

客户端拦截请求:


@Override
public final WebResourceResponse shouldInterceptRequest(final WebView view, WebResourceRequest request) {
    String requestBody = null;
    Uri uri = request.getUrl();

    // 判断是否为Ajax请求(只要链接中包含AJAXINTERCEPT即是)
    if (isAjaxRequest(request)) {
        // 获取post请求参数
        requestBody = getRequestBody(request);
        // 获取原链接
        uri = getOriginalRequestUri(request, MARKER);
    }

    // 重新构造请求,并获取response
    WebResourceResponse webResourceResponse = shouldInterceptRequest(view, new WriteHandlingWebResourceRequest(request, requestBody, uri));
    if (webResourceResponse == null) {
        return webResourceResponse;
    } else {
        return injectIntercept(webResourceResponse, view.getContext());
    }
}

标签:XMLHttpRequest,请求,url,request,send,Android,WebView,prototype,POST
来源: https://www.cnblogs.com/zhenyauntg/p/15672073.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有