Java – Does HttpGet handle cookies automatically

androidcookieshttphttpclientjava

I would like to preserve a session while connecting to server using HttpGet and I need to understand how it handles cookies.

The server developer says that he handles all cookies stuff by himself.
I use HttpGet request to access the server as follows:
InputStream isResponse = null;

    HttpGet httpget = new HttpGet(strUrl);
    HttpResponse response = mClient.execute(httpget);

    HttpEntity entity = response.getEntity();
    isResponse = entity.getContent();
    responseBody = convertStreamToString(isResponse);

    return responseBody;
  1. Should I do something more? Does he put the cookie on my device automatically and the HttpGet method knows to use it in order to keep the sessionID using the cookie?

  2. How can I check if the cookie exist on my device, in order to know if the session is "alive"?

  3. In case I use the following code to get the cookie:

    CookieStore cookieStore = new BasicCookieStore();
    
    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    
    HttpGet httpget = new HttpGet(strUrl);
    HttpResponse response = mClient.execute(httpget,localContext);
    

does the HttpGet still handles the cookies the same as before?

  1. I see that DefaultHttpClient (mClient in the code above) has its own CookieStore. How can I save its cookies and load them next time I create it?

Best Answer

So... After cracking my head for hours and implementing my own primitive CookieStore, I found Android Asynchronous Http Client implementation, that includes a nice PersistentCookieStore that works great! Simply added the jar to my project and used it as follows:

PersistentCookieStore cookieStore = new PersistentCookieStore(context);
DefaultHttpClient mClient = new DefaultHttpClient();    
mClient.setCookieStore(cookieStore);

and that's it! The cookies are saved and reused when the application is open again.

Thank you James Smith, where ever you are. You made at least one man happy.

If anyone interested in my own primitive implementation (that also works) here it is:

package com.pinhassi.android.utilslib;

import java.util.Date;
import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class MyPersistentCookieStore extends BasicCookieStore {
private static final String COOKIES_LIST = "CookiesList";
private static final String COOKIES_NAMES = "CookiesNames";

private Context mContext;
/**
 * 
 */
public MyPersistentCookieStore(Context context) {
    super();
    mContext = context;
    load();
}

@Override
public synchronized void clear(){
    super.clear();
    save();
}

@Override
public synchronized boolean clearExpired(Date date){
    boolean res = super.clearExpired(date);
    save();
    return res;
}

@Override
public synchronized void addCookie(Cookie cookie){
    super.addCookie(cookie);
    save();
}

@Override
public synchronized void addCookies(Cookie[] cookie){
    super.addCookies(cookie);
    save();
}

public synchronized void save()
{
    Editor editor = mContext.getSharedPreferences(COOKIES_LIST, Context.MODE_PRIVATE).edit();
    editor.clear();
    List <Cookie> cookies = this.getCookies();
    StringBuilder sb = new StringBuilder();
    for (Cookie cookie : cookies)
    {
        editor.putString(cookie.getName(),cookie.getValue());
        sb.append(cookie.getName()+";");
    }
    editor.putString(COOKIES_NAMES,sb.toString());
    editor.commit();
}

public synchronized void load()
{
    SharedPreferences prefs = mContext.getSharedPreferences(COOKIES_LIST, Context.MODE_PRIVATE);
    String [] cookies = prefs.getString(COOKIES_NAMES,"").split(";");
    for (String cookieName : cookies){
        String cookieValue = prefs.getString(cookieName, null);
        if (cookieValue!=null)
            super.addCookie(new BasicClientCookie(cookieName,cookieValue));
    }

    }

}