1. 程式人生 > >CefSharp 設定cookie

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