/// <summary> /// Sets a cookie given a valid URL and explicit user-provided cookie attributes. /// This function expects each attribute to be well-formed. It will check for disallowed /// characters (e.g. the ';' character is disallowed within the cookie value attribute) and will return false without setting /// </summary> /// <param name="cookieManager">cookie manager</param> /// <param name="url">The cookie URL. If an empty string is provided, any URL will be matched.</param> /// <param name="cookie">the cookie to be set</param> /// <returns>returns false if the cookie cannot be set (e.g. if illegal charecters such as ';' are used); /// otherwise task that represents the set operation. The value of the TResult parameter contains a bool to indicate success.</returns> public static Task <bool> SetCookieAsync(this CefCookieManager cookieManager, string url, CefNetCookie cookie) { if (cookieManager == null) { throw new NullReferenceException("cookieManager"); } if (cookieManager.IsDisposed) { throw new ObjectDisposedException("cookieManager"); } var callback = new TaskSetCookieCallback(); if (cookieManager.SetCookie(url, cookie, callback)) { return(callback.Task); } //There was a problem setting cookies return(Task.FromResult(false)); }
/// <summary> /// 设置cookies /// </summary> /// <param name="urls"></param> /// <param name="cookies"></param> /// <param name="domain"></param> public static void SetCookie(string domain, Dictionary <string, string> cookies) { CefCookieManager manager = CefCookieManager.GetGlobal(null); string url = "http://" + domain; foreach (var c in cookies) { CefCookie cookie = new CefCookie(); cookie.Name = c.Key; cookie.Value = c.Value; cookie.Domain = domain; cookie.Path = "/"; cookie.HttpOnly = false; //cookie.Expires = DateTime.Now.AddDays(100); //cookie.expires.year = 2200; //cookie.expires.month = 4; //cookie.expires.day_of_week = 5; //cookie.expires.day_of_month = 11; // BSCookieTask bt = new BSCookieTask(url, cookie); //CefRuntime.PostTask(CefThreadId.IO, bt); manager.SetCookie(url, cookie, null); } }