CefSharp ChromiumWebBrowser设置Cookie的方式

.Net技术 码拜 8年前 (2015-12-28) 36060次浏览 0个评论
C# Winform 开发中使用到了开源控件 CefSharp,用于模拟浏览器操作,目标网站需要登录,Cef中有专门的Cookie管理接口,即ICookieManager,设置Cookie的方法在接口中的定义如下:
//
// 摘要:
// 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 the cookie if such
// characters are found.
//
// 参数:
// url:
// The cookie URL
//
// cookie:
// The cookie
Task<bool> SetCookieAsync(string url, Cookie cookie);
以下是CefSharp设置Url Cookie 的具体使用方法:
public static void SetCefCookies(string url, CookieCollection cookies)
{
Cef.GetGlobalCookieManager().SetStoragePath(Environment.CurrentDirectory, true);
foreach (System.Net.Cookie c in cookies)
{
var cookie = new CefSharp.Cookie
{
Creation = DateTime.Now,
Domain = c.Domain,
Name = c.Name,
Value = c.Value,
Expires = c.Expires
};
Task<bool> task = Cef.GetGlobalCookieManager().SetCookieAsync(url, cookie);

while (!task.IsCompleted)
{
continue;
}
bool b = task.Result;
}
}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明CefSharp ChromiumWebBrowser设置Cookie的方式
喜欢 (4)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!