Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all cookies from CookieManager android ?

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

like image 216
mia Avatar asked May 15 '12 15:05

mia


People also ask

What is cookie manager android?

android.webkit.CookieManager. Manages the cookies used by an application's WebView instances. CookieManager represents cookies as strings in the same format as the HTTP Cookie and Set-Cookie header fields (defined in RFC6265bis).

How do I enable cookies on Android WebView?

How do I enable cookies in a webview? CookieManager. getInstance(). setAcceptCookie(true);


1 Answers

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;
    }

}
like image 66
diptia Avatar answered Oct 11 '22 18:10

diptia