You can create a custom WebViewClient
and override the shouldInterceptRequest()
method. If you target API 21 and higher, this will be
override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse?
Inside, you will open an HTTPS connection:
val requestUrl = request.url!!.toString()
val httpsUrl = URL(requestUrl)
val conn: HttpsURLConnection = httpsUrl.openConnection() as HttpsURLConnection
for ((key, value) in request.requestHeaders) {
conn.addRequestProperty(key, value)
}
now you have chance to add new headers or modify the existing ones, e.g.
conn.setRequestProperty("ORIGIN", "https://stackoverflow.com")
when your request is all set, you can wrap the results in a fresh WebResourceResponse:
return WebResourceResponse(
conn.contentType.substringBefore(";"),
conn.contentType.substringAfter("charset=", "UTF-8"),
conn.inputStream)
a typical conn.contentType
is "text/html; charset=UTF-8"
. You may add more resilient parsing, or simply use the hardcoded values if you know them.