【问题标题】:Storing session cookie in android.webkit.CookieManager在 android.webkit.CookieManager 中存储会话 cookie
【发布时间】:2014-11-05 01:52:38
【问题描述】:

我使用 Volley 库来执行我的应用程序的请求。现在我真的需要按照这个顺序执行一些操作:

  • 使用 Volley 库的 POST 请求
  • 我收到带有会话 cookie 的 204 响应
  • 我需要将该 cookie 设置为与 WebView 一起使用

我需要使用 Volley 执行第一个请求,因为响应有一个包含下一个请求的 uri 的标头。比我需要捕获该标题。

问题是我无法使用CookieManager 保存会话cookie,因为正如doc 所说:“正在设置的cookie 必须没有过期并且不能是会话cookie,否则它将是忽略。”。

有没有办法存储该 cookie 以供以后与 WebView 一起使用?

【问题讨论】:

    标签: android cookies webview android-volley


    【解决方案1】:

    奇怪的是,文档要么过时要么完全不正确,似乎CookieManager 将毫无问题地保存会话cookie。 (Here's the bug report)

    这个 sn-p 对我有用:

    private void syncCookie(String domain, Cookie sessionCookie) {
        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeSessionCookie();
        String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain();
        cookieManager.setCookie(domain, cookieString);
        CookieSyncManager.getInstance().sync();
    }
    

    【讨论】:

    • 你是对的。文档过时了!我已经看到 cookie 存储正确!非常感谢
    猜你喜欢
    • 2023-03-25
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    相关资源
    最近更新 更多