我正在使用shouldInterceptRequest拦截来自webview的请求
下面是我返回WebResourceResponse的代码
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static WebResourceResponse handleRequestViaUrlOnly(WebResourceRequest webResourceRequest){ String url = webResourceRequest.getUrl().toString(); Log.i("intercepting req....!!!", url); String ext = MimeTypeMap.getFileExtensionFromUrl(url); String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext); try { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("Sample-Header", "hello"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); return new WebResourceResponse(mime, "UTF-8", conn.getInputStream()); } catch (IOException e) { e.printStackTrace(); } return null; }
我在CustomWebViewClient中调用此方法
class CustomWebViewClient extends WebViewClient { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { return handleRequestViaUrlOnly(request); } }
但是,当我在chrome:// inspect /#devices中检查来自WebView远程调试器的请求标头时.
我添加的附加RequestProperty不存在.
conn.setRequestProperty("Sample-Header", "hello");
WebView中的请求标头中不存在Sample-Header.
我错过了什么吗?我会感激任何帮助.