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)文章
Android中Activity的四種啟動模式和onNewIntent()
android 中activity的啟動模式分為四種,(standard、singleTop、singTask、singleInstance),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2018-08-08android開發(fā)教程之handle實現(xiàn)多線程和異步處理
這篇文章主要介紹了android的handle實現(xiàn)多線程和異步處理的示例,大家參考使用吧2014-01-01NestScrollView嵌套RecyclerView實現(xiàn)淘寶首頁滑動效果
這篇文章主要介紹了NestScrollView嵌套RecyclerView實現(xiàn)淘寶首頁滑動效果,主要實現(xiàn)淘寶首頁嵌套滑動,中間tab吸頂效果,以及介紹NestScrollView嵌套RecyclerView處理滑動沖突的方法,需要的朋友可以參考下2021-12-12Android編程開發(fā)之性能優(yōu)化技巧總結(jié)
這篇文章主要介紹了Android編程開發(fā)之性能優(yōu)化技巧,較為詳細的總結(jié)了Android編程中關(guān)于性能優(yōu)化的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11基于Android實現(xiàn)點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出
這篇文章主要介紹了基于Android實現(xiàn)點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出的相關(guān)資料,需要的朋友可以參考下2015-12-12Android使用AutoCompleteTextView實現(xiàn)自動填充功能的案例
今天小編就為大家分享一篇關(guān)于Android使用AutoCompleteTextView實現(xiàn)自動填充功能的案例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03詳解Android使用Html.fromHtml需要注意的地方
本篇文章主要介紹了詳解Android使用Html.fromHtml需要注意的地方,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07