如何从CookieManager android获取所有cookie? [英] How to get all cookies from CookieManager android ?

查看:955
本文介绍了如何从CookieManager android获取所有cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Android CookieManager 类,有一种方法- getCookie(String url)

为此,我们需要知道正确的URL。

是否可以通过 CookieManager 获取所有cookie并获取url。
诸如 getCookies 之类的东西?
这只是为了再次检查我是否在 getCookie(String url) url 中输入了任何错误>致电。
当我调用相同的代码时,我没有得到cookie。

我在URL中传递了完整的 IP地址。像这样的东西: xx.xxx

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"

谢谢

Mia

Thanks
Mia

推荐答案

我在Android应用程序中将CookieManager与java.net包一起使用,它的工作原理很吸引人。这是一个代码段:

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

}

这篇关于如何从CookieManager android获取所有cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆