Intercept POST requests in a WebView
I was facing the same issue a few days ago.
So I built a library that solves it:
https://github.com/KonstantinSchubert/request_data_webviewclient
It is a WebViewClient with a custom WebResourceRequest that contains the POST/PUT/... payload of XMLHttpRequest requests.
It only works for these though - not for forms and other kind of request sources.
The hack works, basically, by injecting a script into the HTML that intercepts XMLHttpRequest calls. It records the post/put/... content and sends it to an android.webkit.JavascriptInterface
. There, the request is stashed until the shouldInterceptRequest
method is called by Android ...
Use GET instead of POST.
Known issue:http://code.google.com/p/android/issues/detail?id=9122
Was answered here as well:Android - how to intercept a form POST in android WebViewClient on API level 4
I have one of my answers on above thread http://code.google.com/p/android/issues/detail?id=9122
Please see comment#31
Some of the caveats of my solution I see are:
- Putting a dependency on xmlhttprequest prototype which has different implementation for different webkits.
- Security issue in sending data for post requests in URL. But I guess you can solve that through some encryption mechanism.
- URL length issue for some of the browsers if you big data to post
Apart from that, I found this github repo which seems to be solving this problem in another hacky way. I looked into the code but didn't get time to implement and test it. But worth giving a try.