2

How can I access the response headers in Android webview? I searched on Google there is some article about this, but they use OkHTTP or custom HTTP request in shouldOverrideUrlLoading, but I think we need to handle cookies and HTTP methods yourself. I just want to get the response headers.

Is there a simple way to do this? or is there any library for custom webview that I can use to achieve this?

There is a question related to this issue on StackOverflow, but as said he uses an HTTP request in shouldOverrideUrlLoading to send a request to the URL and load the response data into webview.

Access the http response headers in a WebView?

Jeeva
  • 3,975
  • 3
  • 23
  • 47

1 Answers1

1

I searched everywhere but there is not an easy way to solve this, there are different ways to fetch headers. But for my current project, I wrote the following. I hope in the future Android allows inspecting headers in webview.

private fun handleRequestViaOkHttp(url: String) {
    thread {
        try {
            val request = Request.Builder()
                .head()
                .url(url)
                .build()
            val response = httpClient.newCall(request).execute()

            Log.d(TAG, response.headers().toString())
        } catch (e: Exception) {}
    }
}

I am executing this method in shouldInterceptRequest with the URL.

Jeeva
  • 3,975
  • 3
  • 23
  • 47