I need to change the request header of the android webView request. Therefore, I am adding the following code to the shouldInterceptRequest method. 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 requests carry a new header. However, as I create a new request, the original method / body of the request was lost. I do not know how to save the original method and body from WebResourceRequest.
android webview
郑凯 拓
source share