Android 控件WebView设置Cookie

 

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列表的获取根据本身项目的存储方式不一样而不一样。例如使用SharedPreferencesHashMap
  • 注意使用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

/**
 * 获取本地存储的 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
}

03. 域名不一样

使用WebView加载A接口获取到展现的界面,界面须要填充的数据会自动请求B接口。这两接口域名不相同,以前服务器升级的时候搞了一个新的域名。抓包发现AB两个接口域名不一样A为旧 B为新dom

  • 尝试对两个域名设置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);

注释中写到,具备相同的hostpathname的任何现有的Cookie将会被替换为新的Cookie。测试

04. Cookie保存位置

项目中使用WebView其实会自动将Cookie保存在本地数据库中。保存是路径为data/data/package_name/app_WebView/Cookies虽然不是.db结尾的,实际就是一个.db文件this

参考文档

  1. 安卓学习笔记---AndroidWebview里设置Cookie遇到的坑



做者:JustDo23
连接:http://www.jianshu.com/p/c9a9c4e1756d
來源:简书
著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。编码

相关文章
相关标签/搜索