How to get the body of the WebResourceRequest in android webView

浪尽此生 提交于 2020-06-24 08:06:47

问题


I need to modify the request header of the android webView request.So, I add following code in the method shouldInterceptRequest. Here is my code.

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            try {
                    String mUrl = request.getUrl().toString();
                    OkHttpClient httpClient = new OkHttpClient();
                    Request mRequest = new Request.Builder()
                            .url(request.getUrl().toString())
                            .addHeader("token", UserHelper.getToken()) //add headers
                            .build();
                    Response response = httpClient.newCall(mRequest).execute();

                    return new WebResourceResponse(
                            getMimeType(request.getUrl().toString()), // set content-type
                            response.header("content-encoding", "utf-8"),
                            response.body().byteStream()
                    );
            } catch (Exception e) {
                return super.shouldInterceptRequest(view, request);
            }
            return super.shouldInterceptRequest(view, request);
        }

Actually,it works,all the request carry the new header. However, cuz I construct the new request, the original request method/body was lost. I don't know how to keep the original method and body from the WebResourceRequest.

来源:https://stackoverflow.com/questions/46481042/how-to-get-the-body-of-the-webresourcerequest-in-android-webview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!