【CefSharp】CefSharp设置/获取Cookie

在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
喜欢就支持一下吧
点赞5 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容