欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android攔截并獲取WebView內(nèi)部POST請求參數(shù)的實現(xiàn)方法

 更新時間:2019年04月16日 09:00:43   作者:懶星人  
這篇文章主要介紹了Android攔截并獲取WebView內(nèi)部POST請求參數(shù) 的實現(xiàn)方法,本文通過兩種方案給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

起因:

有些時候自家APP中嵌入的H5頁面并不是自家的。但是很多時候又想在H5不知情的情況下獲取H5內(nèi)部請求的參數(shù),這應(yīng)該怎么做到呢?

帶著這個疑問,就有了這篇博客。

實現(xiàn)過程:

方案一:

最開始想到的方案是直接攔截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請求的參數(shù)(因為參數(shù)直接拼在了url鏈接中),對于post請求的參數(shù)無可奈何。

方案二:

后來參考了request_data_webviewclient,有了新的實現(xiàn)方式,具體原理為:給H5注入一段js代碼,目的是在每次Ajax請求都會調(diào)用Android原生的方法,將請求參數(shù)傳給客戶端。

具體流程如下:

 

其中,

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請求參數(shù)
    requestBody = getRequestBody(request);
    // 獲取原鏈接
    uri = getOriginalRequestUri(request, MARKER);
  }
  // 重新構(gòu)造請求,并獲取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);
}

注:根據(jù)

反思:

•開發(fā)過程中遇到了頁面一直顯示不了的問題,實際上就是因為獲取到的mime是"text/html; charset=utf-8",得改成"text/html";

•通過此方法也可篡改response與request,但不要濫用;

•所以說,Android確實不安全!

GitHub地址:webview_post_data

總結(jié)

以上所述是小編給大家介紹的Android攔截并獲取WebView內(nèi)部POST請求參數(shù)的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

最新評論