Session cookie storage in android.webkit.CookieManager
I am using Volley library to fulfill my application requests. Now I really need to do some operations after this order:
- POST request using Volley library.
- I am getting 204 response with session cookie
- I need to set this cookie for use with WebViews
I need to make the first request with Volley because the response has a header containing the uri for the next request. Than I need to capture this title.
The problem is that I cannot save the session cookie using CookieManager
because because doc says "The cookie set must not be expired and there must not be a session cookie, otherwise it will be ignored."
Is there a way to save this cookie for later use with the WebView?
source to share
Oddly enough, the documentation is either out of date or completely wrong, it seems like it CookieManager
will save session cookies without any problem. ( Bug report here )
This snippet works for me:
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();
}
source to share