将会话cookie存储在android.webkit.CookieManager中 [英] Storing session cookie in android.webkit.CookieManager

查看:895
本文介绍了将会话cookie存储在android.webkit.CookieManager中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the Volley library for performing my app's requests. Now I really need to perform some operations following this order:


  • 使用Volley库的POST请求

  • 我收到一个包含会话Cookie的回应

  • 我需要设置要与WebView一起使用的cookie

我需要用Volley执行第一个请求,因为响应有一个头包含下一个请求的uri。

I need to perform the first request with Volley because the response has a header containing the uri for the next request. Than I need to capture that header.

问题是我无法使用 CookieManager 保存会话cookie。 ,因为,作为 doc 说:正在设置的Cookie不能过期,不能为会话Cookie,否则将被忽略。

The problem is that I can not save the session cookie using the CookieManager, because, as the doc says: "The cookie being set must not have expired and must not be a session cookie, otherwise it will be ignored.".

有一种方法可以存储这个cookie以供以后使用WebViews?

Is there a way to store that cookie for later uses with WebViews?

推荐答案

奇怪的是,文档是过时或彻底不正确,似乎 CookieManager 将保存会话cookie,没有任何问题。 (这是错误报告

Strangely enough, the documentation is either out of date or outright incorrect, it seems that the CookieManager will save session cookies with no problems at all. (Here's the bug report)

此代码段适用于我:

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存储在android.webkit.CookieManager中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆