是否可以从Android中的Webview控件访问会话变量(及其值)?
我搜索了文档,但一无所获。
最佳答案
您可以像这样同步Webview会话Cookis
public static void sync(String url) {
List<Cookie> cookies=null;
CookieManager cookieManager = CookieManager.getInstance();
if (cookieManager == null)
return;
RFC2109Spec cookieSpec = new RFC2109Spec();
String rawCookieHeader = null;
try {
URL parsedURL = new URL(url);
rawCookieHeader = cookieManager.getCookie(parsedURL.getHost());
if (rawCookieHeader == null)
return;
int port = parsedURL.getPort() == -1 ? parsedURL.getDefaultPort()
: parsedURL.getPort();
CookieOrigin cookieOrigin = new CookieOrigin(parsedURL.getHost(),
port, "/", false);
cookies = cookieSpec.parse(new BasicHeader("set-cookie",
rawCookieHeader), cookieOrigin);
System.out.println("Session Cookis : " + cookies);
} catch (Exception e) {
}
}
怎么打?从网络视图
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
sync(url);
return super.shouldOverrideUrlLoading(view, url);
}
});