简体   繁体   English

Android Webkit CookieManager:如何通过 CookieManager 获取所有 cookie?

[英]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我正在尝试通过CookieManager.getInstance().getCookie()m.facebook.com 的cookie 加载到android.webkit.WebView ,但它没有给出预期的结果

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.我想使用c_user字段来检查用户是否已登录,以便我可以重定向到自定义 URL。 Viewing cookies of http://m.facebook.com in Google Chrome shows many fields like:在 Google Chrome 中查看http://m.facebook.com 的cookie 会显示许多字段,例如:

c_user datr fr dnonce m_pixel_ratio sb wd x-referer xs

Google Chrome with cookies of m.facebook.com带有 m.facebook.com cookie 的谷歌浏览器

but getCookie() method only return dnonce但是getCookie()方法只返回dnonce

Help would be appriciated.帮助将是appriciated。

**The scenario is I want to develop an Android app that has a single web view. ** 场景是我想开发一个具有单个 Web 视图的 Android 应用程序。 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.当应用程序启动时,我想检查用户是否使用 Facebook 登录,如果是,则将其重定向到某个URL,否则打开 Facebook 登录页面。 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.为了检查用户成功登录,我想出了检查c_user (唯一用户 ID)cookie 的逻辑,但我无法通过getCookie方法获取它。

You can get the cookie value using this您可以使用此获取 cookie 值

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.为所有 cookie 尝试这个类。 CookieManager with the java.net CookieManager 与 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.您需要将确切的 url 传递给CookieManage getCookie方法。 The url you load in WebView loadUrl method.您在WebView loadUrl方法中加载的 url。 For example:例如:

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

Then when you call the method for getting cookies.然后当你调用获取cookie的方法时。

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

And also import the correct CookieManager.并导入正确的 CookieManager。

import android.webkit.CookieManager;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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