在C#中使用CefSharp设置和获取Cookie,你可以通过CookieManager
类来实现。以下是设置和获取Cookie的简单示例代码:
设置Cookie:
var cookieManager = CefSharp.Cef.GetGlobalCookieManager();
cookieManager.SetCookieAsync("http://www.example.com", new CefSharp.Cookie()
{
Domain = ".example.com",
Name = "myCookie",
Value = "myValue",
Expires = DateTime.Now.AddDays(1)
}).ContinueWith(previous =>
{
if (previous.IsCompleted)
{
Console.WriteLine("Cookie set successfully.");
}
else
{
Console.WriteLine("Error setting cookie.");
}
});
获取Cookie:
var cookieManager = CefSharp.Cef.GetGlobalCookieManager();
cookieManager.VisitAllCookiesAsync(new CefSharp.CookieVisitor(cookies =>
{
foreach (var cookie in cookies)
{
Console.WriteLine($"Cookie: {cookie.Domain}, {cookie.Name} = {cookie.Value}");
}
return false; // Return false to continue visiting cookies.
}));
确保在调用CefSharp相关API之前初始化了CefSharp,并且有一个关联的CefSharp.WinForms.ChromiumWebBrowser
实例或者其他CefSharp依赖的初始化。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容