【发布时间】:2012-12-19 14:03:00
【问题描述】:
我正在开发一个过滤请求(带有白名单)并使用自定义 SSLSocketFactory 的 Android 应用程序。为此,我开发了一个自定义的WebViewClient 并覆盖了shouldInterceptRequest 方法。我可以过滤并将我的SocketFactory 与 GET 请求一起使用,但我无法拦截 POST 请求。
那么,有没有办法拦截WebView 中的 POST 请求?
这里是 shouldInterceptRequest 方法的代码:
public final WebResourceResponse shouldInterceptRequest(WebView view, String urlStr) {
URI uri = URI.create(urlStr);
String scheme = uri.getScheme();
// If scheme not http(s), let the default webview manage it
if(!"http".equals(scheme) && !"https".equals(scheme)) {
return null;
}
URL url = uri.toURL();
if(doCancelRequest(url)) {
// Empty response
Log.d(TAG, "URL filtered: " + url);
return new WebResourceResponse("text/plain", "UTF-8", new EmptyInputStream());
} else {
Log.d(TAG, "URL: " + url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", mSettings.getUserAgentString());
// Configure connections
configureConnection(conn);
String mimeType = conn.getContentType();
String encoding = conn.getContentEncoding();
if(mimeType != null && mimeType.contains(CONTENT_TYPE_SPLIT)) {
String[] split = mimeType.split(CONTENT_TYPE_SPLIT);
mimeType = split[0];
Matcher matcher = CONTENT_TYPE_PATTERN.matcher(split[1]);
if(matcher.find()) {
encoding = matcher.group(1);
}
}
InputStream is = conn.getInputStream();
return new WebResourceResponse(mimeType, encoding, is);
}
}
【问题讨论】:
-
您是否遇到错误,或者它根本不起作用?您也应该发布一些相关代码,例如您覆盖的
shouldInterceptRequest方法。 -
不,我没有错误。我在 shouldInterceptRequest 方法的开头记录了 url,我只看到 GET 请求。其他的似乎在 WebView 低级别管理。
标签: android post webview request