Android拦截并获取WebView内部POST请求参数的示例分析

小编给大家分享一下Android拦截并获取WebView内部POST请求参数的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

站在用户的角度思考问题,与客户深入沟通,找到五台网站设计与五台网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广、域名申请、虚拟空间、企业邮箱。业务覆盖五台地区。

起因:

有些时候自家APP中嵌入的H5页面并不是自家的。但是很多时候又想在H5不知情的情况下获取H5内部请求的参数,这应该怎么做到呢?

带着这个疑问,就有了这篇博客。

实现过程:

方案一:

最开始想到的方案是直接拦截H5中所有的请求:

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原生的方法,将请求参数传给客户端。

具体流程如下:

 Android拦截并获取WebView内部POST请求参数的示例分析

其中,

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());
  }
}

客户端注入js代码:

private WebResourceResponse injectIntercept(WebResourceResponse response, Context context) {
  String encoding = response.getEncoding();
  String mime = response.getMimeType();
  // WebResourceResponse的mime必须为"text/html",不能是"text/html; charset=utf-8"
  if (mime.contains("text/html")) {
    mime = "text/html";
  }
  InputStream responseData = response.getData();
  InputStream injectedResponseData = injectInterceptToStream(
      context,
      responseData,
      mime,
      encoding
  );
  return new WebResourceResponse(mime, encoding, injectedResponseData);
}

注:根据谷歌官方文档,mime必须为"text/html"。

Android拦截并获取WebView内部POST请求参数的示例分析

反思:

•开发过程中遇到了页面一直显示不了的问题,实际上就是因为获取到的mime是"text/html; charset=utf-8",得改成"text/html";

•通过此方法也可篡改response与request,但不要滥用;

•所以说,Android确实不安全!

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

以上是“Android拦截并获取WebView内部POST请求参数的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!

当前题目:Android拦截并获取WebView内部POST请求参数的示例分析
文章源于:https://www.cdcxhl.com/article24/isjjje.html

成都网站建设公司_创新互联,为您提供网站设计公司静态网站网站设计电子商务网站收录网站内链

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设