Syncing cookies between an HttpClient and a WebView

Hey all,

Today I would like to show how to share cookies between an HttpClient and a WebView. This is necessary when your application use an HttpClient to make POST/GET calls, and then opens a WebView to a webpage that needs the same session information. Say that we are using a DefaultHttpClient named client.


DefaultHttpClient client;

//gets all cookies from the HttpClient's cookie jar
List<Cookie> cookies = client.getCookieStore().getCookies();

		if (! cookies.isEmpty()){

			CookieSyncManager.createInstance(YourContext.this);
			CookieManager cookieManager = CookieManager.getInstance();

                        //sync all the cookies in the httpclient with the webview by generating cookie string
			for (Cookie cookie : cookies){
				
				sessionInfo = cookie;
	
					String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
					cookieManager.setCookie(YOUR_DOMAIN, cookieString);
					CookieSyncManager.getInstance().sync();
			}
		}

Make sure that YOUR_DOMAIN corresponds to the domain of the website you need session info from, else this won’t work.

On StackOverflow there is a similar solution, but I found that calling cookieManager.removeSessionCookie() makes it often not work for me (in particular if I got a new session cookie by logging out/changing user). I’m not sure why, since in the thread they have the opposite happening. But, the above code has never failed for me.

Advertisements
This entry was posted in Android and tagged , , , . Bookmark the permalink.

19 Responses to Syncing cookies between an HttpClient and a WebView

  1. Manjunath Siddartha says:

    Thanks Sooo Much.. Saved My Life 🙂 🙂

  2. Arrchana says:

    What is sessionInfo? its declaration is not given in the code here.

  3. Nadu says:

    Thank you so much… you save my live too. 🙂

  4. coasterleung says:

    it can’t work in android 4.0?

  5. You are very right about cookieManager.removeSessionCookie() I was having problem. I removed this line and it works like a charm! Thanks! 🙂

  6. Ali says:

    Portsmouth-born Guadalupe is enthusiastic about ugg
    norway fine art, tea tasting. Usually, the words “UGG Australia” are printed in the middle of the lid (top portion of box).
    One theory is that people these days are just plain lazy.

  7. Karan says:

    Can u give me a complete code of this example i already spent 3 day but now resolve
    my server side pdf url is http://www.xxx.com?=wqewq not .pdf pls share complete where can i put to share same session webview and http client

  8. The fruit is even a wise alternative to laxatives for anyone experiencing constipation.
    not substituting an entire meal for a gimmick, but actually
    learning to make meals for ourselves that are delicious, filling, and
    help our bodies lose weight quickly. Luckily Asian women get
    thin and burn fat easily, even after giving birth, and do it
    in a healthy way that anyone can learn.

  9. sesame oil says:

    Use the organic, untoasted sesame oil, or try
    virgin olive oil, apricot kernel oil, or avocado oil if you want varied moisturizing and vitamin effects.
    Sesame contains a powerful antioxidant system, consisting mainly of three substances, sesamin, sesaminol and vitamin C (in the form of gamma-tocopherol).
    It has the function of making the vocal cords elastic and flexible.

  10. almond oil says:

    Other popular choices are jojoba oil, olive oil,
    and almond oil. It is a major organ of your body, and is, in fact, the heaviest
    organ by weight, and also the largest by surface area. Sometimes these patches
    tend to get itchy, and of course there’s the temptation to scratch, but this only makes the problem worse and increases the chance of infection.

  11. www.nndb.com says:

    He says a skepticism honed in 20 years of experience working as New York State fire marshals are also eligible for a PI license?
    When you are looking for would be to reclassify all computer crimes as Federal crimes, then
    all these investigations could be handled routinely in one
    central location. As a private investigator, there are also cases
    where complete strangers target victims. Why do the interview?
    Hensley: Well, the trial was so long.

  12. Its like you learn my thoughts! You seem to understand so much about this, like you wrote
    the guide in it or something. I feel that you just could do with a
    few % to pressure the message home a bit, however instead of that, that is great blog.
    A great read. I will certainly be back.

  13. Riccardo says:

    You saved my life! It is working perfectly! thanks! ;D

  14. Francisco says:

    where is your WebView code ?, we appreciate it if you post it !, let me know…

  15. sex says:

    Very energetic article, I liked that bit. Will there be a part
    2?

  16. Alex says:

    You are a genius man.!!!!

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