简体   繁体   English

在 Android WebView 中访问 HTTP 响应头

[英]Accessing HTTP Response Headers in Android WebView

How can I access the response headers in Android webview?如何访问 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.我在 Google 上搜索了一些关于此的文章,但他们在shouldOverrideUrlLoading使用OkHTTP或自定义 HTTP 请求,但我认为我们需要自己处理 cookie 和 HTTP 方法。 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?或者是否有任何自定义 webview 库可以用来实现这一目标?

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. StackOverflow 上有一个与此问题相关的问题,但正如所说他在shouldOverrideUrlLoading使用 HTTP 请求向 URL 发送请求并将响应数据加载到 webview 中。

Access the http response headers in a WebView? 访问 WebView 中的 http 响应标头?

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.我希望将来 Android 允许在 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.我正在使用 URL 在shouldInterceptRequest执行此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM