在web视图拦截POST请求 [英] Intercept POST requests in a WebView
问题描述
我的深化发展Android应用程序过滤请求(用白名单),并使用自定义的 SSLSocketFactory的
。对于这一点,我已经开发了一个自定义的 WebViewClient
,我已经覆盖了 shouldInterceptRequest
方法。我可以过滤,用我的的SocketFactory
的GET请求,但我不能拦截POST请求。
那么,有没有办法拦截在POST请求的WebView
?
下面是shouldInterceptRequest方法的code:
公众最终WebResourceResponse shouldInterceptRequest(web视图查看,串urlStr){
URI URI = URI.create(urlStr);
字符串模式= uri.getScheme();
//如果方案未HTTP(S),让默认的WebView管理它
如果(HTTP.equals(路线)及!&安培;!的https.equals(方案)){
返回null;
}
网址URL = URI.toURL()根据;
如果(doCancelRequest(URL)){
//空响应
Log.d(TAG,URL过滤:+网址);
返回新WebResourceResponse(text / plain的,UTF-8,新EmptyInputStream());
} 其他 {
Log.d(TAG,网址:+网址);
HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();
conn.setRequestProperty(用户代理,mSettings.getUserAgentString());
//配置连接
configureConnection(康涅狄格州);
字符串MIMETYPE = conn.getContentType();
字符串编码= conn.getContentEncoding();
如果(MIMETYPE = NULL和放大器;!&安培; mimeType.contains(CONTENT_TYPE_SPLIT)){
的String []分割= mimeType.split(CONTENT_TYPE_SPLIT);
MIMETYPE =分割[0];
匹配器匹配= CONTENT_TYPE_PATTERN.matcher(分裂[1]);
如果(matcher.find()){
编码= matcher.group(1);
}
}
InputStream的是= conn.getInputStream();
返回新WebResourceResponse(MIMETYPE,编码,就是);
}
}
使用GET而不是POST。
已知问题: HTTP://$c$c.google.com/p /安卓/问题/详细信息?ID = 9122
在这里回答为好: <一href="http://stackoverflow.com/questions/3664440/android-how-to-intercept-a-form-post-in-android-webviewclient-on-api-level-4">Android - 如何拦截安卓WebViewClient表单POST的API级别4
I'm developping an Android application filtering the requests (with a white list) and using a custom SSLSocketFactory
. For this, I've developed a custom WebViewClient
and I have overridden the shouldInterceptRequest
method. I can filter and use my SocketFactory
with the GET requests but I can't intercept the POST requests.
So, is there a way to intercept the POST requests in a WebView
?
Here is the code of the shouldInterceptRequest method :
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);
}
}
Use GET instead of POST.
Known issue: http://code.google.com/p/android/issues/detail?id=9122
Was answered here as well: Android - how to intercept a form POST in android WebViewClient on API level 4
这篇关于在web视图拦截POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!