无法在Android WebView中设置HttpURLConnection标头

时间:2018-08-09 20:14:32

标签: java android httpurlconnection

我试图在执行请求之前拦截WebView发送的每个请求并修改标头。有很多关于如何执行此操作的示例(like this one),但我无法完全实现它。页面能够正常加载,但是所有请求在Chrome调试器中看起来像this;他们警告说“已显示临时标头”,而我的标头都不存在。

这是我代码的相关部分:

  .s1 {
    top: 50px;
    position: absolute;
    background: #BF1C1A;
    color: white;
    h5 {
      padding: 5px;
      line-height: 20px;

    }  
  }

我也尝试使用OkHttp,但是得到了相同的结果。任何人都能使它正常工作吗?

2 个答案:

答案 0 :(得分:0)

请尝试执行此操作,而应使用shouldOverrideUrlLoading而不是shouldInterceptRequest

public class MyWebView extends WebView {

    public MyWebView(Context context) {
        super(context);
        setWebViewClient(new MyWebViewClient());
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
        {
            final String url = request.getUrl().toString();
            return getNewResponse(url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            return getNewResponse(url);
        }

        private WebResourceResponse getNewResponse(String url) {
            final OkHttpClient httpClient = new OkHttpClient();
            Response response = null;
            try {
                final Request request = new Request.Builder()
                        .url(url.trim())
                        .addHeader("test-header", "test-value")
                        .build();
                response = httpClient.newCall(request).execute();
            }
            catch (Exception e) {
            }
            if (response == null) {
                return null;
            }
            return new WebResourceResponse(
                    getMimeType(url),
                    response.header("content-encoding", "utf-8"),
                    response.body().byteStream()
            );
        }

        private String getMimeType(String url) {
            final String extension = MimeTypeMap.getFileExtensionFromUrl(url);

            if (extension == null) {
                return null;
            }
            switch (extension) {
                case "js":
                    return "text/javascript";
                case "woff":
                    return "application/font-woff";
                case "woff2":
                    return "application/font-woff2";
                case "ttf":
                    return "application/x-font-ttf";
                case "eot":
                    return "application/vnd.ms-fontobject";
                case "svg":
                    return "image/svg+xml";
                case "mp4":
                    return "video/mp4";
            }

            return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        }
    }
}

答案 1 :(得分:0)

事实证明,我的代码实际上运行良好。问题似乎是Chrome网络检查器无法访问通过HttpURLConnection(或WebView本身之外的其他任何东西)发送的请求的请求元数据,因此我的标头没有显示在那里。但是,使用我鞭打过的this tool将所有请求标头显示为json,我可以看到它们实际上是按预期传递的。