同步的问题很常见,网上搜到的解决方法基本相似。数据库
/** * 给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. }
这里简单说明:服务器
https://www.baidu.com/
就可使www.baidu.com
SharedPreferences
和HashMap
key=value
的完整形式。文档提示the cookie as a string, using the format of the 'Set-Cookie' HTTP response header
CookieSyncManager.getInstance().sync();
方法的替代方法是cookieManager.flush();
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; }
在这里记录一下使用SharedPreferences
保存整个Cookie串并使用HashMap
存储键值对cookie
/** * 获取本地存储的 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的时候须要进行编码。app
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 }
使用WebView加载A接口获取到展现的界面,界面须要填充的数据会自动请求B接口。这两接口域名不相同,以前服务器升级的时候搞了一个新的域名。抓包发现AB两个接口域名不一样A为旧 B为新
。dom
进行了一番搜索以后,发现有在强调,只有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。测试
项目中使用WebView其实会自动将Cookie保存在本地数据库中。保存是路径为data/data/package_name/app_WebView/Cookies
虽然不是.db
结尾的,实际就是一个.db
文件this
做者:JustDo23
连接:http://www.jianshu.com/p/c9a9c4e1756d
來源:简书
著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。编码