简体   繁体   English

在Android中的ShouldInterceptRequest中的WebView中获取响应标头

[英]Get response headers in webview in shouldInterceptRequest in Android

I am trying to get response headers in webview when I post some url to the server. 当我向服务器发布一些URL时,我试图在webview中获取响应头。 I am using shouldInterceptRequest method. 我正在使用shouldInterceptRequest方法。

@Override 
        public WebResourceResponse shouldInterceptRequest(final WebView view, final WebResourceRequest request) {

            if(request.getUrl().toString().contains(SMConstant.INTERCEPTED_URL)){
                if(interceptFlag==0){
                    ((Activity) mContext).runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            view.postUrl(request.getUrl().toString(), EncodingUtils.getBytes(postData, "UTF-8"));
                        }
                    });
                    interceptFlag++;
                }

            }
            return super.shouldInterceptRequest(view, request);
        }

This method return WebResourceResponse object. 此方法返回WebResourceResponse对象。 But I am not getting the way how to get the response headers out of it. 但是我没有办法从中获取响应头。

By default return super.shouldInterceptRequest(view, request); 默认情况下return super.shouldInterceptRequest(view, request); returns null. 返回null。

so , what should be done so that actual webview response should be captured. 因此,应该采取什么措施才能捕获实际的Webview响应。

Try this code(require API VERSION 21): 尝试以下代码(需要API版本21):

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            if (request.getUrl().toString().contains("some_char")) {// condition to intercept webview's request
                return handleIntercept(request);
            } else
                return super.shouldInterceptRequest(view, request);
        }
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private WebResourceResponse handleIntercept(WebResourceRequest request){
    OkHttpClient okHttpClient = new OkHttpClient();
    final Call call = okHttpClient.newCall(new Request.Builder()
            .url(request.getUrl().toString())
            .method(request.getMethod(),null)
            .headers(Headers.of(request.getRequestHeaders()))
            .build()
    );
    try {
        final Response response = call.execute();
        response.headers();// get response header here
        return new WebResourceResponse(
                response.header("content-type", "text/plain"), // You can set something other as default content-type
                response.header("content-encoding", "utf-8"),  //you can set another encoding as default
                response.body().byteStream()
        );
    } catch (IOException e) {
        e.printStackTrace();
        return null
    }
}

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

https://artemzin.com/blog/use-okhttp-to-load-resources-for-webview/ https://artemzin.com/blog/use-okhttp-to-load-resources-for-webview/

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

相关问题 Android WebView loadDataWithBaseURL shouldInterceptRequest - Android WebView loadDataWithBaseURL shouldInterceptRequest Android:WebView shouldInterceptRequest不在WebView中添加RequestProperties - Android: WebView shouldInterceptRequest not adding RequestProperties in the WebView Android webView shouldInterceptRequest - 停止加载资源 - Android webView shouldInterceptRequest - Stop Loading Resource 何时在android webview中调用shouldInterceptRequest() - When does shouldInterceptRequest() is called in android webview Android WebView使用shouldInterceptRequest覆盖即时崩溃 - Android WebView instant crash with shouldInterceptRequest override 检查 webview 的 shouldInterceptRequest 是否会被调用? - To check if webview's shouldInterceptRequest will get called or not? 在Android Webview中阻止视频-ShouldInterceptRequest()的替代方法 - Block videos in android webview - Alternative to shouldInterceptRequest() 在 Android WebView 中访问 HTTP 响应头 - Accessing HTTP Response Headers in Android WebView 如何将带有“重定向”代码的 WebResourceResponse 传递给 WebViewClient.shouldInterceptRequest 中的 Android WebView? - How to pass WebResourceResponse with "redirect" code to Android WebView in WebViewClient.shouldInterceptRequest? Android 从未在 MAUI 中调用 WebViewClient 的 ShouldInterceptRequest WebView - Android WebViewClient's ShouldInterceptRequest is never called in MAUI WebView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM