To keep cookies after each request in HttpURLConnection, should to add CookieHandler on the app starting:
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
But in the app closing and opening again the cookies are empty... So how to save the cookies after the closing?
Something like save them in the SharedPreferences or in the file and get them back after the opening...
I tryed to keep them using CookieStore, but that's not worked:...
Save:
Settings.Save(c, TAG, cookieManager.getCookieStore().getCookies().toString());
Load:
String load = Settings.Load(c, TAG);
if (load != null) {
for (HttpCookie hc : HttpCookie.parse(load)) {
cookieManager.getCookieStore().add(new URI(Data.domain), hc);
}
}
Thanks..
The default CookieStore
does not persist anything to disk, you need to implement one which does. Here is an example implementation which saves Cookies directly to SharedPreferences.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With