My R&D World

All Things R&D

Cookie Management in Android Web Apps Development

I write this article because cookie management was a big issue which I spent quite some time on it in my GSoC2012 project but Google provides little reference.

Google engineers suggested using HttpURLConnection which would be a trend instead of Apache HTTP Client in this blog. However, HttpURLConnection has far less APIs than Apache HTTP Client which makes new programmers a little confused on how to manage cookies during HTTP request-response transactions. Even the reference of HttpURLConnection provides little useful materials on this field but only suggest using vm-wide cookie manager.

Actually, android.webkit has a very useful cookie manager which makes our programming much easier. Its full name is “android.webkit.CookieManager”.

Below is the code script which I use for cookie management in HTTP request-response.

HttpURLConnection urlConnection = null;
try {
    urlConnection = (HttpURLConnection) url.openConnection();

    // Set cookies in requests
    CookieManager cookieManager = CookieManager.getInstance();
    String cookie = cookieManager.getCookie(urlConnection.getURL().toString());
    if (cookie != null) {
        urlConnection.setRequestProperty("Cookie", cookie);
    }
    urlConnection.connect();

    // Get cookies from responses and save into the cookie manager
    List cookieList = urlConnection.getHeaderFields().get("Set-Cookie");
    if (cookieList != null) {
        for (String cookieTemp : cookieList) {
            cookieManager.setCookie(urlConnection.getURL().toString(), cookieTemp);
        }
    }

    InputStream in = new BufferedInputStream (urlConnection.getInputStream());
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
    }
}

Line 6 returns a singleton CookieManager so you don’t have to worry about cookie lost when using it in different places. Line 14 is to get cookies if the response message has multiple “Set-Cookie” fields.

Cookies are critical in login system of many websites. Hope this article will help you.

About these ads

3 responses to “Cookie Management in Android Web Apps Development

  1. Pingback: GSoC 2012 Weekly Report on OI WebIntents Agent (June 11-17) « My R&D World

  2. Sujith February 13, 2014 at 10:59

    Thnks for the article Cheng Zheng .I want to know how you get the cookies after restarting the application.so is there any option to save the cookies in database or somewhere?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: