01. 设置方式
同步的问题很常见,网上搜到的解决方法基本类似。
/**
* 给WebView同步Cookie
*
* @param context 上下文
* @param url 可以使用[domain][host]
*/
private void syncCookie(Context context, String url) {
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();// 移除旧的[可以省略]
List<HttpCookie> cookies = new PersistentCookieStore(context).getCookies();// 获取Cookie[可以是其他的方式获取]
for (int i = 0; i < cookies.size(); i++) {
HttpCookie cookie = cookies.get(i);
String value = cookie.getName() + "=" + cookie.getValue();
cookieManager.setCookie(url, value);
}
CookieSyncManager.getInstance().sync();// To get instant sync instead of waiting for the timer to trigger, the host can call this.
}
这里简单说明:
- 参数中的URL在使用过程中基本是域名。例如
https://www.baidu.com/
就可以使www.baidu.com
- Cookie列表的获取根据自己项目的存储方式不同而不同。例如使用
SharedPreferences
和HashMap
- 注意使用for循环进行setCookie(String url, String value)调用。网上有博客表示使用分号手动拼接的value值会导致cookie不能完整设置或者无效
- 注意value的值是使用
key=value
的完整形式。文档提示the cookie as a string, using the format of the 'Set-Cookie' HTTP response header
- CookieSyncManager是个过时的类,Api21中WebView可以自动同步。
CookieSyncManager.getInstance().sync();
方法的替代方法是cookieManager.flush();
- Cookie同步方法要在WebView的setting设置完之后调用,否则无效。
- 调用
loadUrl(url);
前一句调用此方法进行Cookie同步操作。
/**
* 获取URL的域名
*/
private String getDomain(String url){
url = url.replace("http://", "").replace("https://", "");
if (url.contains("/")) {
url = url.substring(0, url.indexOf('/'));
}
return url;
}
02. 保存Cookie
在这里记录一下使用SharedPreferences
保存整个Cookie串并使用HashMap
存储键值对
/**
* 获取本地存储的 Cookie 集合
*
* @return Cookie 键值对
*/
public Map<String, String> getCookieMap() {
Map<String, String> cookieMap = new HashMap<>();
String cookie = getCookie();// 从SharedPreferences中获取整个Cookie串
if (!TextUtils.isEmpty(cookie)) {
String[] cookieArray = cookie.split(";");// 多个Cookie是使用分号分隔的
for (int i = 0; i < cookieArray.length; i++) {
int position = cookieArray[i].indexOf("=");// 在Cookie中键值使用等号分隔
String cookieName = cookieArray[i].substring(0, position);// 获取键
String cookieValue = cookieArray[i].substring(position + 1);// 获取值
cookieMap.put(cookieName, NetCodeUtil.encodeURL(cookieValue));// 存至Map
// 解码使用 URLEncoder.encode(str, "UTF-8");
}
}
return cookieMap;
}
注意:编解码,从请求头中获取到的Cookie是经过URL 编码的,解码后可以获取到姓名之类中文,在给WebView或者是其他请求设置Cookie的时候需要进行编码。
CookieUtil cookieUtil = new CookieUtil(this);// 将 Cookie 保存在了 SharedPreferences
Map<String, String> cookieMap = cookieUtil.getCookieMap();// 获取键值对
for (Map.Entry<String, String> entry : cookieMap.entrySet()) {// 遍历 Map
String value = entry.getKey() + "=" + entry.getValue();// 键值对拼接成 value
cookieManager.setCookie(url, value);// 设置 Cookie
}
03. 域名不同
使用WebView加载A接口获取到展示的界面,界面需要填充的数据会自动请求B接口。这两接口域名不相同,之前服务器升级的时候搞了一个新的域名。抓包发现AB两个接口域名不同A为旧 B为新
。
- 尝试对两个域名设置Cookie测试发现不没有效果
- 尝试将两个接口的域名设置为相同的测试发现两个接口都携带了Cookie
进行了一番搜索之后,发现有在强调,只有cookie的domain和path与请求的URL匹配才会发送这个cookie。
/**
* Sets a cookie for the given URL. Any existing cookie with the same host,
* path and name will be replaced with the new cookie. The cookie being set
* will be ignored if it is expired.
*
* @param url the URL for which the cookie is to be set
* @param value the cookie as a string, using the format of the 'Set-Cookie'
* HTTP response header
*/
public abstract void setCookie(String url, String value);
注释中写到,具有相同的host
,path
和name
的任何现有的Cookie将会被替换为新的Cookie。
04. Cookie保存位置
项目中使用WebView其实会自动将Cookie保存在本地数据库中。保存是路径为data/data/package_name/app_WebView/Cookies
虽然不是.db
结尾的,实际就是一个.db
文件
参考文档
作者:JustDo23
链接:http://www.jianshu.com/p/c9a9c4e1756d
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。