How to get all cookies from CookieManager android ?
Asked Answered
V

2

17

For Android CookieManager class there is a method -- getCookie(String url).
For this we need to know correct url.
Is there a way to get all cookies in CookieManager and get the urls . some thing like getCookies ?? This is just to double check if i am giving anything wrong in my url for getCookie(String url) call. I am not getting the cookie when i call the same.
I am passing complete IP address here in url. Something like this : "xx.x.x.x"

Thanks
Mia

Valles answered 15/5, 2012 at 15:29 Comment(1)
i am trying to extract cookie from webview after succesful login- its simple -- String cookieString = CookieManager.getInstance().getCookie("xx.x.x.x);Valles
U
7

I used the CookieManager with the java.net package in my Android Application and it works like a charm. Here is a code snippet :

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.util.List;

private class MyCookieManager
{       
    private CookieManager mCookieManager = null;

    MyCookieManager() {
        mCookieManager = new CookieManager();
        mCookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(mCookieManager);
    }

    private List<HttpCookie> getCookies() {
        if(mCookieManager == null)
            return null;
        else
            return mCookieManager.getCookieStore().getCookies();
    }

    public void clearCookies() {
        if(mCookieManager != null)
            mCookieManager.getCookieStore().removeAll();
    } 

    public boolean isCookieManagerEmpty() {
        if(mCookieManager == null)
            return true;
        else 
            return mCookieManager.getCookieStore().getCookies().isEmpty();
    }


    public String getCookieValue() {
        String cookieValue = new String();

        if(!isCookieManagerEmpty()) {
            for (HttpCookie eachCookie : getCookies())
                cookieValue = cookieValue + String.format("%s=%s; ", eachCookie.getName(), eachCookie.getValue());
        }

        return cookieValue;
    }

}
Unchancy answered 29/4, 2014 at 12:46 Comment(2)
@stan when i want to use getCookieValue() ,i have to create object of MyCookieManager class and constructer will be called and every time new instant of CookieManager will be generated with no cookie saved. How can i retrieve cookie from default CookieManager where i previously stored cookie ?Iridium
@HarshalBhatt, it's supposed that you create and keep a single instance of MyCookieManager in your application. Anyway, all instances should normally return the same set of cookies from default cookie store (or a custom store if your'll change the source to provide specific store in CookieManager constructor).Virgate
T
-1

You can use reflection to see the cookie map. It's called mCookieMap in 4.0.3 (and probably in earlier releases as well). The type is Map>.

This isn't a great way of doing it because you'll risk breaking on different devices or OS versions if they don't use mCookieMap, but CookieManager doesn't offer a public way of knowing which URLs it's visited.

Tadashi answered 14/8, 2012 at 18:51 Comment(2)
The cookies in 4.2.2 are stored natively, so this will not work: androidxref.com/4.2.2_r1/xref/frameworks/base/core/java/android/…Spoilsman
I knew it'd break eventually. :-)Tadashi

© 2022 - 2024 — McMap. All rights reserved.