acsbendi / android-request-inspector-webview Goto Github PK
View Code? Open in Web Editor NEWInspect and intercept full HTTP requests sent from Android WebViews
License: MIT License
Inspect and intercept full HTTP requests sent from Android WebViews
License: MIT License
Hello! I'm loading a webpage that is always doing the same POST request and most of the times I'm able to get request body using this library. However, sometimes when shouldInterceptRequest() is called, webViewRequest.type is HTML instead of XML_HTTP and in that case POST request body is not available. Please note that webViewRequest.method is always POST as expected.
Any idea of what might be happening and how to fix this? Thank you
Hello! I've been using the following code to make calls and noticed that most of the requests made by the WebView are showing up in my console. However, some requests are not appearing in my console. Why is this happening?
**
RequestInspectorWebViewClient requestInspector = new RequestInspectorWebViewClient(webview);
webview.setWebViewClient(requestInspector);**
Getting a rare crash in Crashlytics (library version 1.0.2):
Exception: java.util.ConcurrentModificationException:
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1029)
at java.util.ArrayList$Itr.next(ArrayList.java:982)
at com.acsbendi.requestinspectorwebview.RequestInspectorJavaScriptInterface.findRecordedRequestForUrl(RequestInspectorJavaScriptInterface.kt:21)
at com.acsbendi.requestinspectorwebview.RequestInspectorWebViewClient.shouldInterceptRequest(RequestInspectorWebViewClient.kt:29)
at WV.la.a(chromium-TrichromeWebViewGoogle.aab-stable-584517231:80)
at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.aab-stable-584517231:15)
There is an ArrayList in the library source code for RequestInspectorJavaScriptInterface:
private val recordedRequests = ArrayList<RecordedRequest>()
I think there should probably be a CopyOnWriteArrayList or maybe a ConcurrentMap instead of ArrayList, or an immutable collection, or maybe another way to ensure thread-safety for correct behavior of concurrent find() and add() calls.
I tried to use OkHttp to provide fake response for all requests:
webViewClient = object : RequestInspectorWebViewClient(this@apply) {
private val httpClient = OkHttpClient()
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun shouldInterceptRequest(
view: WebView,
webViewRequest: WebViewRequest
): WebResourceResponse {
val request = Request.Builder()
.url(BASE_URL + webViewRequest.url)
.method(
webViewRequest.method,
(if (HttpMethod.permitsRequestBody(webViewRequest.method)) webViewRequest.body else null
?.toRequestBody(webViewRequest.headers["content-type"]?.toMediaTypeOrNull())
)
.headers(webViewRequest.headers.toHeaders())
.build()
val response = try {
httpClient.newCall(request).execute()
} catch (e: Exception) {
Log.e("WebView", "Request failed at " + request.url)
return WebResourceResponse(
"text/plain",
"utf-8",
InputStream.nullInputStream()
)
}
Log.d(
"WebView",
"${response.protocol} ${response.request.method} ${response.request.url}"
)
val type = response.headers["content-type"]?.toMediaTypeOrNull()
return WebResourceResponse(
if (type != null)
"${type.type}/${type.subtype}"
else null,
response.headers["content-encoding"] ?: "utf-8",
response.code,
response.message.ifEmpty { getDefaultMessage(response.code) },
response.headers.toMultimap().mapValues { it.value.first() },
response.body?.byteStream()
)
}
}
But I got this error from time to time:
FATAL EXCEPTION: main
Process: com.example.browser, PID: 14726
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.next(ArrayList.java:860)
at com.acsbendi.requestinspectorwebview.RequestInspectorJavaScriptInterface.findRecordedRequestForUrl(RequestInspectorJavaScriptInterface.kt:21)
at com.acsbendi.requestinspectorwebview.RequestInspectorWebViewClient.shouldInterceptRequest(RequestInspectorWebViewClient.kt:29)
at K9.a(chromium-TrichromeWebViewGoogle6432.aab-beta-535902244:80)
at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle6432.aab-beta-535902244:15)
First of all, I deeply appreciate this very good project.
I wonder If a changed request does not make any problem when I change some body content.
ex.
override fun shouldInterceptRequest( view: WebView, webViewRequest: WebViewRequest ): WebResourceResponse? {
webViewRequest.body = changedBodyString
return super.shouldInterceptRequest(view, webViewRequest) }
recorded request fetched with condition url.contains(recordedRequest.url)
, but if url = "https://somedomain.com/some/paths/here"
and there was a request with url https://somedomain.com/some/path
it will give wrong request.
Fix might be comparing extensively all recorded requests with all options.
It would be good to have a feature to capture the response body as well.
Desired Syntax
val response = super.shouldInterceptRequest(view, webViewRequest)
println(response.body)
return response
failed to set the 'responsetype' property on 'xmlhttprequest': the response type cannot be changed for synchronous requests made from a document.
webview load url https://hkjx.hhplayer.com/index.php?url=2005AE78846FFAB6ABDC629D04C8F045519E0864A194AC397D3797783B7700D5
return body html contain js like
$.post("api.php",{"url":url,"t":t,"key":key,"act":act,"play":play},function(data){
webViewRequest.body() got empty which expected the post json string, how to fix this?
Had try the fork git https://github.com/daentech/Android-Request-Inspector-WebView, but no luck, got empty body too.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.