小編給大家分享一下Android攔截并獲取WebView內(nèi)部POST請求參數(shù)的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
站在用戶的角度思考問題,與客戶深入溝通,找到五臺網(wǎng)站設(shè)計(jì)與五臺網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋五臺地區(qū)。
起因:
有些時候自家APP中嵌入的H5頁面并不是自家的。但是很多時候又想在H5不知情的情況下獲取H5內(nèi)部請求的參數(shù),這應(yīng)該怎么做到呢?
帶著這個疑問,就有了這篇博客。
實(shí)現(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ù)(因?yàn)閰?shù)直接拼在了url鏈接中),對于post請求的參數(shù)無可奈何。
方案二:
后來參考了request_data_webviewclient,有了新的實(shí)現(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ù)谷歌官方文檔,mime必須為"text/html"。
反思:
?開發(fā)過程中遇到了頁面一直顯示不了的問題,實(shí)際上就是因?yàn)楂@取到的mime是"text/html; charset=utf-8",得改成"text/html";
?通過此方法也可篡改response與request,但不要濫用;
?所以說,Android確實(shí)不安全!
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
以上是“Android攔截并獲取WebView內(nèi)部POST請求參數(shù)的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前題目:Android攔截并獲取WebView內(nèi)部POST請求參數(shù)的示例分析
文章源于:http://muchs.cn/article24/isjjje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)、電子商務(wù)、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)