0

Searched a lot.

I have an app. App logins on server and receive some cookies, then it can execute some POST requests with them (e.g. to get user profile). I want to store them between sessions (it means I can restart a device, run app and get profile without extra logging in). Or, in other words, how to create persistent cookie storage?

I'm using the only DefaultHttpClient and there are no WebViews. But I should initialize this client after creation with some cookies. I should store it in file or what? Are there ways to do it in iOS way without weird hacks, storing in files/DBs and manual filling CookieManagers?

efpies
  • 3,625
  • 6
  • 34
  • 45

3 Answers3

4

Now I'm using a PersistentCookieStorage class for those purposes. I create a singleton when the app is launched. It stores cookies in SharedPreferences.

efpies
  • 3,625
  • 6
  • 34
  • 45
0

An implementation of java.net.CookieStore for providing persistent cookies.

Use this https://gist.github.com/manishk3008/2a2373c6c155a5df6326

Manish
  • 1,946
  • 2
  • 24
  • 36
0

Storing some data in CookieManager:

void populateCookieStore(URI uri)
        throws IOException {

    CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);

    CookieHandler.setDefault(cm);

    Map<String,List<String>> header = new HashMap<>();

    List<String> values = new ArrayList<>();

    values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
               + URI_PATH +"; HttpOnly");

    values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);

    header.put("Set-Cookie", values);

    cm.put(uri, header);
}
Stephen
  • 9,899
  • 16
  • 90
  • 137