androidcookiesandroidhttpclientcookiestore

: Persistent cookies used in HttpClient and HttpGet access


I would like the user to perform different tasks via HttpGet/HttpPost actions. I would like to save the Cookies so that the user only needs to login after expiration of the cookie.

When traversing the internet I saw that the "PersistentCookiestore" is a good starting point.

Question 1: how can I use the (apache) PersistentCookieStore in the HttpClient software? I don't see complete examples, e.g. how to start using the PersistentCookieStore in the first httpclient use.

See for example:

static PersistentCookieStore cookie_jar = new PersistentCookieStore( getApplicationContext()); 

public void login() { 
    // how to connect the persistent cookie store to the HttpClient? 
    ....
    client2 = new DefaultHttpClient( httpParameters);
    …
    client2.setCookieStore(cookie_jar);
    ....
    HttpGet method2 = new HttpGet(uri2);
    ....
    try {
     res = client2.execute(method2); 
}
catch ( ClientProtocolException e1) { e1.printStackTrace(); return false; }
    ....

Question 2: How can I update the cookie after a call, or is this never needed? In other words: When I have to update the cookie after a call to HttpGet or HttpPost after calling the client2.execute( ...).

In the example code of (non-persistent) cookies with httpclient I saw:

cookie_jar = client.getCookieStore(); 
….
HttpGet or HttpPost … 
client.setCookieStore( ....) 
client.execute( .. )  // second call

Thank you for helping.


Solution

  • The original question was: how do I prevent login each time after app start? Answer is of course to use cookies. So how do I store cookies so I can reuse after app restart?

    The answer that works for me is very simple: just convert the cookiestore to a string, put it in the shared preferences before exiting the app. After the next app start, so before the next login, just get the sting from the shared preferences, convert it back to a cookiestore. Using the cookiestore prevents me logging in again.

    public void saveCookieStore( CookieStore cookieStore) {
        StringBuilder cookies = new StringBuilder();
        for (final Cookie cookie : cookieStore.getCookies()) {
            cookies.append(cookie.getName());
            cookies.append('=');
            cookies.append(cookie.getValue());
            cookies.append('=');
            cookies.append(cookie.getDomain());
            cookies.append(';');
        }
        SharedPreferences.Editor edit = sharedPref.edit();
        edit.putString( MY_GC_COOKIESTORE, cookies.toString());
        edit.commit();
    }
    
    // retrieve the saved cookiestore from the shared pref
    public CookieStore restoreCookieStore() {
        String savedCookieString = sharedPref.getString( MY_GC_COOKIESTORE, null);
        if( savedCookieString == null || savedCookieString.isEmpty()) { 
            return null; 
        }
        CookieStore cookieStore = new BasicCookieStore();
        for ( String cookie : StringUtils.split( savedCookieString, ';')) {
            String[] split = StringUtils.split( cookie, "=", 3);
            if( split.length == 3) {
                BasicClientCookie newCookie = new BasicClientCookie( split[0], split[1]);
                newCookie.setDomain( split[2]);
                cookieStore.addCookie( newCookie);
            }
        }
        return cookieStore;
    }