简体   繁体   中英

Android Webkit CookieManager: How to get all cookies via CookieManager?

I'm trying to get cookies of m.facebook.com loaded in android.webkit.WebView via CookieManager.getInstance().getCookie() but it's not giving the expected results

myWebView.loadUrl("http://m.facebook.com");

String cookies = CookieManager.getInstance().getCookie("m.facebook.com");

I want to use c_user field to check whether user is logged in so I can redirect to custom URL. Viewing cookies of http://m.facebook.com in Google Chrome shows many fields like:

c_user datr fr dnonce m_pixel_ratio sb wd x-referer xs

Google Chrome with cookies of m.facebook.com

but getCookie() method only return dnonce

Help would be appriciated.

**The scenario is I want to develop an Android app that has a single web view. When app starts I want to check if user is logged in with Facebook, if yes then redirect it to some URL otherwise open Facebook login page. To check user successful login I came up with the logic to check c_user (unique user id) cookie but I can't get it via getCookie method.

You can get the cookie value using this

getCookie("http://www.example.com","cookieName");

Declare the function as

public String getCookie(String siteName,String cookieName){     
    String CookieValue = null;

    CookieManager cookieManager = CookieManager.getInstance();
    String cookies = cookieManager.getCookie(siteName);       
    String[] temp=cookies.split(";");
    for (String ar1 : temp ){
        if(ar1.contains(cookieName)){
            String[] temp1=ar1.split("=");
            CookieValue = temp1[1];
            break;
        }
    }              
    return CookieValue; 
}

Try this class for all cookies. CookieManager with the java.net

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

    }

You need to pass the the exact url to the CookieManage getCookie method. The url you load in WebView loadUrl method. For example:

webview.loadUrl("https://m.facebook.com");

Then when you call the method for getting cookies.

getCookie("https://m.facebook.com");

And also import the correct CookieManager.

import android.webkit.CookieManager;

public String getCookie(String site) {
    CookieManager cookieManager = CookieManager.getInstance();
    String cookies = cookieManager.getCookie(site);
    return cookies;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM