premacck / request_data_webviewclient

Android WebView with a custom WebResourceRequest that contains the outgoing payload of AJAX requests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request Data - WebViewClient

Android WebViewClient with a custom WebResourceRequest that contains the POST/PUT/... payload of XMLHttpRequest requests

This project is inspired by https://github.com/KeejOow/android-post-webview and draws some code from there. Also, this project is a kotlin conversion of https://github.com/KonstantinSchubert/request_data_webviewclient

When you need to display a webview to the user on which you need to intercept the HTTP calls and perform them yourself (for example to add additional security), you can do so on Android by registering a WebViewClient and implementing

fun shouldInterceptRequest(webview: WebView?, request: WebResourceRequest?): WebResourceResponse?

Unfortunately, the request object passed to this method does not contain any POST data.

This project provides a hack around this limitation by injecting a script into the HTML that intercepts AJAX calls.

It provides the WriteHandlingWebViewClient class that extends WebViewClient and provides the

fun shouldInterceptRequest(view: WebView?, request: WriteHandlingWebResourceRequest?): WebResourceResponse?

method to override.

WriteHandlingWebResourceRequest extends WebResourceRequest, but provides the crucial method

public String getAjaxData()

which returns the data in the request body of the AJAX request.

Installation

  • Add the writeinterceptingwebview folder as a libarary module to your android project.
  • jCenter-Maven package will come as soon as I figure out how to do that ... Say what you want about pip, at least it's easy to upload a package.

Example usage

webView.webViewClient = object : WriteHandlingWebViewClient(webView) {
   override fun shouldInterceptRequest(view: WebView?, request: WriteHandlingWebResourceRequest?): WebResourceResponse? {
        // works the same as WebViewClient.shouldOverrideUrlLoading, 
        // but you have request.getAjaxData() which gives you the 
        // request body
   }
}

What about forms? What about other kind of http requests?

The library can probably be extended to work for forms too. In fact, https://github.com/KeejOow/android-post-webview shows how it's done. However, I wasn't happy with my adaption of this so far. Pull requests are welcome ...

About

Android WebView with a custom WebResourceRequest that contains the outgoing payload of AJAX requests


Languages

Language:Kotlin 74.2%Language:Java 14.3%Language:HTML 11.5%