- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 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 远程调试器的请求 header 时。
我添加的额外 RequestProperty 不存在。
conn.setRequestProperty("Sample-Header", "hello");
WebView 的请求 header 中不存在 Sample-Header。
我错过了什么吗?我将不胜感激。
最佳答案
所以问题是,当您传递 conn.getInputStream()
时,它只提供数据。可以通过 conn.getHeaderFields()
提取响应 header 。此外,除非服务器支持它并且CORS,否则您将无法取回额外的 header 。没有参与。这是连接的 wireshark
输出
GET /~fdc/sample.html HTTP/1.1
Sample-Header: hello
Content-Type: application/x-www-form-urlencoded
User-Agent: Dalvik/2.1.0 (Linux; U; Android 7.1; Android SDK built for x86_64 Build/NPF26K)
Host: www.columbia.edu
Connection: Keep-Alive
Accept-Encoding: gzip
Content-Length: 0
HTTP/1.1 200 OK
Date: Wed, 01 Mar 2017 09:06:58 GMT
Server: Apache
Last-Modified: Thu, 22 Apr 2004 15:52:25 GMT
Accept-Ranges: bytes
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 8664
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: text/html
如您所见,响应中没有 Sample-Header: hello
header 。
下面是从响应构建 WebResourceResponse
header 并将您的自定义 header 附加到它的简单代码:
webView.setWebViewClient(new WebViewClient() {
private Map<String, String> convertResponseHeaders(Map<String, List<String>> headers) {
Map<String, String> responseHeaders = new HashMap<>();
responseHeaders.put("Sample-Header", "hello");
for (Map.Entry<String, List<String>> item : headers.entrySet()) {
List<String> values = new ArrayList<String>();
for (String headerVal : item.getValue()) {
values.add(headerVal);
}
String value = StringUtil.join(values, ",");
Log.e(TAG, "processRequest: " + item.getKey() + " : " + value);
responseHeaders.put(item.getKey(), value);
}
return responseHeaders;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
final String method = request.getMethod();
final String url = request.getUrl().toString();
Log.d(TAG, "processRequest: " + url + " method " + method);
String ext = MimeTypeMap.getFileExtensionFromUrl(url);
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
try {
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("Sample-Header", "hello");
conn.setDoInput(true);
conn.setUseCaches(false);
Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());
responseHeaders.put("Sample-Header", "hello");
return new WebResourceResponse(
mime,
conn.getContentEncoding(),
conn.getResponseCode(),
conn.getResponseMessage(),
responseHeaders,
conn.getInputStream()
);
} catch (Exception e) {
Log.e(TAG, "shouldInterceptRequest: " + e);
}
return null;
}
});
关于Android:WebView shouldInterceptRequest 不在 WebView 中添加 RequestProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41869163/
我正在尝试将我的 Java 应用程序连接到位于 https://api.guildwars2.com/v2/ 的公共(public)游戏 API 为此我写了下面的代码: url = new URL(U
我无法从 Twitch.TV API v3 ( https://github.com/justintv/Twitch-API/blob/master/v3_resources/streams.md )
我正在使用 shouldInterceptRequest 拦截来自 webview 的请求 下面是我返回 WebResourceResponse 的代码 @TargetApi(Build.VERSIO
我写了一个简单的下载管理器,我正在尝试为所有下载设置 RESUME。在谷歌搜索如何做到这一点之后。我知道必须 setRequestProperty 才能连接,但我的代码不起作用,我收到此错误: FAT
我是一名优秀的程序员,十分优秀!