CEF3 获取Cookie例子 CefCookieManager C++

首先从cef_cookie.h 源码中看到CefCookieManager 这个类:

  // Visit all cookies on the IO thread. The returned cookies are ordered by
  // longest path, then by earliest creation date. Returns false if cookies
  // cannot be accessed.
  ///
  /*--cef()--*/
  virtual bool VisitAllCookies(CefRefPtr<CefCookieVisitor> visitor) =0;

  ///
  // Visit a subset of cookies on the IO thread. The results are filtered by the
  // given url scheme, host, domain and path. If |includeHttpOnly| is true
  // HTTP-only cookies will also be included in the results. The returned
  // cookies are ordered by longest path, then by earliest creation date.
  // Returns false if cookies cannot be accessed.
  ///
  /*--cef()--*/
  virtual bool VisitUrlCookies(const CefString& url,
                               bool includeHttpOnly,
                               CefRefPtr<CefCookieVisitor> visitor) =0;
 1 class CefCookieVisitor : public virtual CefBase {
 2  public:
 3   ///
 4   // Method that will be called once for each cookie. |count| is the 0-based
 5   // index for the current cookie. |total| is the total number of cookies.
 6   // Set |deleteCookie| to true to delete the cookie currently being visited.
 7   // Return false to stop visiting cookies. This method may never be called if
 8   // no cookies are found.
 9   ///
10   /*--cef()--*/
11   virtual bool Visit(const CefCookie& cookie, int count, int total,
12                      bool& deleteCookie) =0;
13 };

可以通过VisitAllCookies获取所有cookies;VisitUrlCookies获取域名下的所有cookies。

看到VisitUrlCookies的参数是CefCookieVisitor;所以实现一个类用于回调读取cookies;

class CCookieVisitor : public CefCookieVisitor
{
public:
    CCookieVisitor() {};
    ~CCookieVisitor() {};

    bool Visit(const CefCookie& cookie, int count, int total,
        bool& deleteCookie);
    //这是一个宏
    //所有的框架类从CefBase继承,实例指针由CefRefPtr管理,CefRefPtr通过调用AddRef()和Release()方法自动管理引用计数。
    IMPLEMENT_REFCOUNTING(CookieVisitor);
};        
//作为类的成员变量CefRefPtr<CCookieVisitor> m_CookieVisitor;
m_CookieVisitor(new CCookieVisitor());

 //以下代码执行 即回调Visit

 CefRefPtr<CefCookieManager> cefCookieManager = CefCookieManager::GetGlobalManager(nullptr);

if (cefCookieManager)
  {
    cefCookieManager->VisitUrlCookies(url ,true , m_visitor);
  }

回调进行读取,count为当前cookie total为总数。具体看CefCookieVisitor的注释,接下来便可以Visit读取到数据

 1 bool CookieVisitor::Visit(const CefCookie & cookie, int count, int total, bool & deleteCookie)
 2 {
 3     if (count == total)
 4     {
 5         return false;
 6     }
 7     if (cookie.name.str && cookie.value.str)
 8     {
 9         string strName = cookie.name.str;
10         string strValue = cookie.value.str;
11     }
12     return true;
13 }

结束!

时间: 2024-11-08 22:19:50

CEF3 获取Cookie例子 CefCookieManager C++的相关文章

用WebCollector 2.x爬取新浪微博(无需手动获取cookie)

用WebCollector 2.x 配合另一个项目WeiboHelper,就可以直接爬取新浪微博的数据(无需手动获取cookie) 1.导入WebCollector 2.x和WeiboHelper的所有jar包 两个项目的地址:http://git.oschina.net/webcollector/WebCollector http://git.oschina.net/webcollector/WeiboHelper 2.示例代码: package cn.edu.hfut.dmic.webcol

【学习笔记】JavaScript设置与获取Cookie

获取Cookie: 1 function getCookie(cookiename) { 2 var result; 3 var mycookie = document.cookie; 4 var start2 = mycookie.indexOf(cookiename + "="); 5 if (start2 > -1) { 6 start = mycookie.indexOf("=", start2) + 1; 7 var end = mycookie.i

设置cookie、获取cookie、删除cookie函数封装

//设置cookie函数function setCookie(key,value,t){   var oDate = new Date();   oDate.setDate( oDate.getDate() + t );   document.cookie = key + '=' + encodeURI(value) + ';expires=' + oDate.toGMTString();} //获取cookie函数function getCookie(key){ //通过分号空格拆分开所有co

如何使用js来获取cookie的值

如何使用js来获取cookie的值 读取属于当前文档的所有cookies var allcookies = document.cookie; 定义一个函数,用来读取特定的cookie值. function getCookie(cookie_name) { var allcookies = document.cookie; var cookie_pos = allcookies.indexOf(cookie_name);   //索引的长度 // 如果找到了索引,就代表cookie存在, // 反

js获取cookie值

js获取cookie值,代码如下: 1 function getCookie(key) { 2 var arr = document.cookie.split(';'); 3 var obj = new Object(); 4 for(var i = 0; i < arr.length; i++) { 5 var tmp = arr[i].split('='); 6 obj[tmp[0]] = tmp[1]; 7 } 8 return obj[key]; 9 } 调用: 1 alert(getC

javascript设置和获取cookie值

//设置cookie function setCookie(name,value,expires,path,domain,secure){ var cookieName = encodeURIComponent(name) + '=' + encodeURIComponent(value); if(expires instanceof Date){ cookieName += ';expires=' + expires; } if(path){ cookieName += ';path=' +

jQuery获取cookie

之前一直以为获取cookie的方法封装在了jQuery包中...没想到还得单独下jquery.cookie.js插件,不太好找,备份一份: /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2013 Klaus Hartl * Released under the MIT license */ (function (factory) { if (typeof de

javascript设置和获取cookie的通用方法

//获取cookie  function getCookieValue(cookieName)  {     var cookieValue = document.cookie;     var cookieStartAt = cookieValue.indexOf(""+cookieName+"=");     if(cookieStartAt==-1)     {         cookieStartAt = cookieValue.indexOf(cooki

PHP获取Cookie模拟登录

一.定义Cookie存储路径 必须使用绝对路径 $cookie_jar = dirname(__FILE__)."/pic.cookie"; //$cookie_file = dirname(__FILE__).'/cookie.txt'; //$cookie_file = tempnam("tmp","cookie"); 二.获取Cookie $url = "http://1.2.3.4/"; $ch = curl_init