CefSharp COOKIE保留

public ChromiumWebBrowser CWebBrowser;
public CookieVisitor cookieVisitor;
public static string cookies;
public Form1()
        {
            InitializeComponent();
            InitBrowser();
        }
public void InitBrowser()
        {
            var cefSettings = new CefSettings()
            {
                IgnoreCertificateErrors = true,
                CachePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cache"),
                LogSeverity = LogSeverity.Info,
                LogFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt")
            };
            Cef.Initialize(cefSettings);
            CWebBrowser = new ChromiumWebBrowser(RegUrl);
            this.Controls.Add(CWebBrowser);
            CWebBrowser.Dock = DockStyle.Fill;
            CWebBrowser.FrameLoadStart += CWebBrowser_FrameLoadStart; ;
            CWebBrowser.FrameLoadEnd += CWebBrowser_FrameLoadEnd; ;
            CWebBrowser.RequestHandler = new Requesthandler();
            cookies=ios.read("cookie");
            var cookieManager = CefSharp.Cef.GetGlobalCookieManager();
            string[] arr = cookies.Split('$');
            foreach (string s in arr)
            {
                if (string.IsNullOrEmpty(s))
                    continue;
                string[] car = s.Split('^');
                CefSharp.Cookie cookie = new CefSharp.Cookie();
                cookie.Domain = car[0];
                cookie.Name = car[1];
                cookie.Value = car[2];
                cookieManager.SetCookieAsync("http://" + cookie.Domain, cookie);
            }
        }
private void CWebBrowser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            var cookieManager = CefSharp.Cef.GetGlobalCookieManager();
            CookieVisitor visitor = new CookieVisitor();
            visitor.SendCookie += visitor_SendCookie;
            cookieManager.VisitAllCookies(visitor);
        }
private void visitor_SendCookie(CefSharp.Cookie obj)
        {
            cookies += obj.Domain.TrimStart('.') + "^" + obj.Name + "^" + obj.Value + "$";
            ios.write("cookie", cookies);
        }
public static CookieCollection GetCookieCollection()
        {
            CookieCollection cookieCollection = new CookieCollection();
            string[] arr = cookies.Split('$');
            foreach (string s in arr)
            {
                if (string.IsNullOrEmpty(s))
                    continue;
                string[] car = s.Split('^');
                System.Net.Cookie cookie = new System.Net.Cookie();
                cookie.Domain = car[0];
                cookie.Name = car[1];
                cookie.Value = car[2];
                cookieCollection.Add(cookie);
            }
            return cookieCollection;
        } 

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

发表评论