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 集合
 *
 * @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);

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

04. Cookie保存位置

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

参考文档

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

升级

引言:实际使用中遇到问题并进行更新。

时间:2018年04月19日21:03:12

作者:JustDo23

01. 针对一级域名

  • 不用对每个具体的 Url 进行设置 Cookie
  • 针对项目使用的一级域名进行设置 Cookie
  • 选择合适的时机进行刷新

 

public void synCookies(Context context) {
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    CookieSyncManager.createInstance(context);
  }
  CookieManager cookieManager = CookieManager.getInstance();
  cookieManager.setAcceptCookie(true);// 允许接受 Cookie
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.removeSessionCookie();// 移除
  } else {
    cookieManager.removeSessionCookies(null);// 移除
  }
  List<HttpCookie> cookies = UserModle.getInstance(this).getCookies();
  for (int i = 0; i < cookies.size(); i++) {
    HttpCookie cookie = cookies.get(i);
    String value = cookie.getName() + "=" + cookie.getValue();
    cookieManager.setCookie(".baidu.com", value);
  }
  cookieManager.setCookie(".baidu.com", "Domain=.baidu.com");
  cookieManager.setCookie(".baidu.com", "Path=/");
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    CookieSyncManager.getInstance().sync();
  } else {
    cookieManager.flush();
  }
}

02. UserAgent

  • 每次设置 UserAgent 会导致 WebView 重新加载
  • 根据需要进行设置
  • 注意各个添加分号

 

String original = webView.getSettings().getUserAgentString();// 获取 WebView 的 UserAgent
original += " Version:" + versionName + ";";// 替换
webView.getSettings().setUserAgentString(original);// 设置新的 UserAgent

03. 参考

  1. 选择性参考

項目使用案例:

      web = (WebView) findViewById(R.id.wv_meet_detail);

        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setAllowFileAccess(true);
        web.getSettings().setPluginState(WebSettings.PluginState.ON);
        web.getSettings().setUseWideViewPort(true);
        web.getSettings().setLoadWithOverviewMode(true);
        web.getSettings().setTextSize(WebSettings.TextSize.LARGEST);//设置字体大小
        web.setWebViewClient(new WebViewClient());

        web.setBackgroundColor(Color.parseColor("#FAFAFA")); // 设置背景色
        web.getBackground().setAlpha(255); // 设置填充透明度 范围:0-255
        String url = "http://192.168.11.188:80/Web/t_guidance_document_Info.aspx";
        synCookies(this,url);
        web.loadUrl(url);




  /**
     * 同步一下cookie
     */
    public static void synCookies(Context context, String url) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        cookieManager.removeSessionCookie();//移除
        String cookies = SharedPreferencesUtils.getString("cookie");
        cookieManager.setCookie(url, cookies);//cookies是在HttpClient中获得的cookie
        CookieSyncManager.getInstance().sync();
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值