CefSharp存储Cookie和读Cookie

背景:需要在Winform客户端程序中存储Cookie,并可以取出来:

1.存储Cookie

 var cookieManager = CefSharp.Cef.GetGlobalCookieManager();
 var domain="www.baidu.com";
 cookieManager.SetCookie("http://"+domain, new CefSharp.Cookie
                                {
                                    Domain= domain,
                                    Name ="formText",//Cookie名称
                                    Value= station_name,//Cookie值
                                    Expires=DateTime.Now.AddDays(7)//过期时间
                                });

2.取Cookie

(1)需要先继承接口 ICookieVisitor

public class CookieVisitor : CefSharp.ICookieVisitor  
    {  
        public event Action<CefSharp.Cookie> SendCookie;  
        public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie)  
        {  
            deleteCookie = false;  
            if (SendCookie != null)  
            {  
                SendCookie(cookie);  
            }  

            return true;  
        }  
    } 

(2)定义事件

string cookies="";
private void visitor_SendCookie(CefSharp.Cookie obj)
        {
            cookies+=obj.Name+":"+obj.Value;
            if(obj.Name="userid"){
                //选取指定cookie
            }
        }

(3)取Cookie

CookieVisitor visitor = new CookieVisitor();
visitor.SendCookie += visitor_SendCookie;//注册事件
var cookieManager = CefSharp.Cef.GetGlobalCookieManager();
cookieManager.VisitAllCookies(visitor);//访问所有cookie,这一步一定要有
//  cookieManager.VisitUrlCookies("http://www.baidu.com", true, visitor);//访问指定网站下Cookie

就可以取出来了

上一篇:Python机器学习(Sebastian著 ) 学习笔记——第六章模型评估与参数调优实战(Windows Spyder Python 3.6)


下一篇:Encoder-Decoder LSTM模型对家庭用电进行多步时间序列预测(多变量输入)