自定义webview中的请求头

安卓代码:


            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                if (!url.startsWith(domain)) {
                    return super.shouldInterceptRequest(view, request);
                }
                Request.Builder reqBuilder = new Request.Builder()
                        .url(url)
                        .method(request.getMethod(), null);
                Map<String, String> headers = request.getRequestHeaders();
                for (Map.Entry<String, String> e : headers.entrySet()) {
                    reqBuilder.header(e.getKey(), e.getValue());
                }
                reqBuilder.header("Authorization", "Basic TGl****************w==");
                try {
                    Response resp = okHttpClient.newCall(reqBuilder.build()).execute();
                    InputStream bodyStream;

                    ResponseBody body = resp.body();
                    if (url.startsWith(domain + "/web/config/index.js")) {
                        String result = xhr_intercept_js + body.string();
                        bodyStream = new ByteArrayInputStream(result.getBytes());
                    } else {
                        bodyStream = body.byteStream();
                    }

                    MediaType mediaType = body.contentType();
                    String mineType = null;
                    String encoding = null;
                    if (mediaType != null) {
                        mineType = mediaType.toString();
                        Charset charset = mediaType.charset();
                        if (charset != null) {
                            encoding = charset.displayName();
                        }
                    }

                    return new WebResourceResponse(mineType, encoding, resp.code(), resp.message(), convertHeaders(resp.headers().toMultimap()), bodyStream);
                } catch (IOException e) {
                    e.printStackTrace();
                    return super.shouldInterceptRequest(view, request);
                }
            }

            private Map<String, String> convertHeaders(Map<String, List<String>> headers) {
                HashMap<String, String> result = new HashMap<>();
                for (Map.Entry<String, List<String>> e : headers.entrySet()) {
                    result.put(e.getKey(), e.getValue().get(0));
                }
                return result;
            }

其中,字符串变量xhr_intercept_js的值为:

        ;(function($) {
            if (!$.XMLHttpRequest.prototype.reallySend) {
                $.XMLHttpRequest.prototype.reallySend = $.XMLHttpRequest.prototype.send;
                $.XMLHttpRequest.prototype.send = function(body) {
                    this.setRequestHeader('Authorization', 'Basic TGl****************w==');
                    this.reallySend(body);
                }
            }
        })(window);

评论