CefSharp使用教程三(Cookie處理)

設置cookie

var cookieManager = CefSharp.Cef.GetGlobalCookieManager();  
await cookieManager.SetCookieAsync("http://" + domain, new CefSharp.Cookie(  
{  
    Domain = domain,  
    Name = name,  
    Value = value,  
    Expires = DateTime.MinValue  
});

讀取cookie
建立Cookie讀取對象,繼承接口 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;  
        }  
    }  
    /// 回調事件
    private void visitor_SendCookie(CefSharp.Cookie obj)  
    {  
        cookies += obj.Domain.TrimStart('.') + "^" + obj.Name + "^" + obj.Value + "$";  
    }  

加載指定頁面的cookie信息

 /// <summary>
        /// html頁面加載完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        async void webBrowser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            string _url = e.Url;
            //判斷是否是需要獲取cookie的頁面
            if(_url.Contains("你頁面的url連接地址")){
                  //註冊獲取cookie回調事件
                 CookieVisitor visitor = new CookieVisitor();
                 visitor.SendCookie += visitor_SycmCookie;
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章