Get response headers in webview in shouldInterceptRequest in Android

10,967

Try this code(require API VERSION 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?

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

Share:
10,967

Related videos on Youtube

Shantanu Jha
Author by

Shantanu Jha

Updated on June 04, 2022

Comments

  • Shantanu Jha
    Shantanu Jha almost 2 years

    I am trying to get response headers in webview when I post some url to the server. I am using shouldInterceptRequest method.

    @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. But I am not getting the way how to get the response headers out of it.

    By default return super.shouldInterceptRequest(view, request); returns null.

    so , what should be done so that actual webview response should be captured.

    • Sunil Sunny
      Sunil Sunny about 8 years
      WebView doesn't provide access to response headers . You have to use a HTTP client and retrieve the page yourself if you need to access this data. stackoverflow.com/questions/3134389/…
  • Arya
    Arya over 6 years
    Do you know how to get the request body for a POST from the "WebResourceRequest request"?