• cefSharp 获取和设置 cookie


    一 看一下界面:

    button名 :  button1

    textbox名:  textBox_cookie

    二 html页面

    1. <html>
    2. <head>
    3. <title>page1title>
    4. head>
    5. <body>
    6. this is page 111
    7. <br><a href="page4.html">to page4a>
    8. <script language=javascript>
    9. document.cookie="username=Tom&age=22";
    10. script>
    11. body>
    12. html>

    三 代码

    两个方案

    1,获取js内容的方式

    1. private void button1_Click(object sender, EventArgs e)
    2. {
    3. Task<CefSharp.JavascriptResponse> t = webBrower.EvaluateScriptAsync("document.cookie");
    4. t.Wait();
    5. var cookie = t.Result.Result;
    6. }

    2,cefSharp的标准方式

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. this.Load += Form1_Load;
    7. }
    8. ChromiumWebBrowser webBrower = null;
    9. string cookies;
    10. private void Form1_Load(object sender, EventArgs e)
    11. {
    12. //初始化配置
    13. string path = "";
    14. webBrower = new ChromiumWebBrowser(path);
    15. webBrower.Dock = DockStyle.Fill;// 填充方式
    16. webBrower.FrameLoadEnd += WebBrower_FrameLoadEnd;
    17. button1.Click += button1_Click;
    18. this.Controls.Add(webBrower);
    19. //加载测试页面
    20. webBrower.Load("http://localhost/page1.html");
    21. //模拟写入cookie(测试页面,也有一个cookie)
    22. var cookieManager = CefSharp.Cef.GetGlobalCookieManager();
    23. cookieManager.SetCookieAsync("http://localhost", new CefSharp.Cookie()
    24. {
    25. Domain = "localhost",
    26. Name = "1",
    27. Value = "2",
    28. Expires = DateTime.MinValue
    29. });
    30. }
    31. //加载完成后,获取cookie
    32. private void WebBrower_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
    33. {
    34. //注册获取cookie回调事件
    35. ICookieManager cookieManager = webBrower.GetCookieManager();
    36. CookieVisitor visitor = new CookieVisitor();
    37. visitor.SendCookie += visitor_SendCookie;
    38. cookieManager.VisitAllCookies(visitor);
    39. }
    40. /// 回调事件
    41. private void visitor_SendCookie(CefSharp.Cookie obj)
    42. {
    43. cookies += obj.Domain.TrimStart('.') + "^" + obj.Name + "^" + obj.Value + "\r\n";
    44. }
    45. //cefsharp 获取cookie的方法
    46. public class CookieVisitor : CefSharp.ICookieVisitor
    47. {
    48. public event Action SendCookie;
    49. public void Dispose()
    50. {
    51. }
    52. public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie)
    53. {
    54. deleteCookie = false;
    55. if (SendCookie != null)
    56. {
    57. SendCookie(cookie);
    58. }
    59. return true;
    60. }
    61. }
    62. //cookie放到text_cookie上
    63. private void button1_Click(object sender, EventArgs e)
    64. {
    65. this.textBox_cookie.Text = cookies;
    66. }
    67. }

    PS:如果想在FrameLoadEnd事件中显示

    那么代码需要委托,

    1 Load增加如下

    1. public Action GetCookie;
    2. private void Form1_Load(object sender, EventArgs e)
    3. {
    4. //绑定获取cookie的委托
    5. GetCookie = () =>
    6. {
    7. Task t = webBrower.EvaluateScriptAsync("document.cookie");
    8. t.Wait();
    9. var cookie = t.Result.Result;
    10. this.textBox_cookie.Text = cookie.ToString();
    11. };
    12. }

    2 WFrameLoadEnd增加如下

    1. private void WebBrower_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
    2. {
    3. this.BeginInvoke(GetCookie);
    4. }

  • 相关阅读:
    释放搜索潜力:基于ES(ElasticSearch)打造高效的语义搜索系统,让信息尽在掌握[1.安装部署篇--简洁版],支持Linux/Windows部署安装
    计算机毕业设计Java电子配件公司仓库管理系统(源码+系统+mysql数据库+lw文档)
    什么是软件工程?
    【无标题】
    DNS(二)
    完了!这 57 道面试题(美团、BAT、携程),我咋一个都不会?
    Web服务器项目实战(一)
    python flask 接入 sentry
    最全解决方式java.net.BindException Address already in use JVM_Bind
    【Mybatis】动态 SQL
  • 原文地址:https://blog.csdn.net/hanjun0612/article/details/125917874