- 先同步再loadUrl
syncCookies()
mWebView.loadUrl(url);
- 同步方法
/**
* 同步Cookies
*/
private void syncCookies() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
CookieSyncManager.createInstance(this);
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeSessionCookie();
} else {
cookieManager.removeSessionCookies(null);
}
String baseUrl = Constant.getBaseUrl();
String host = Uri.parse(baseUrl).getHost();
Set<String> cookies = CookiesUtils.getInstance().getCookie(baseUrl, host);
for (String cookie : cookies) {
cookieManager.setCookie(host, cookie);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
CookieSyncManager.getInstance().sync();
} else {
cookieManager.flush();
}
}
- cookies获取方式
response.headers("Set-Cookie");
网友评论