赞助

CEF 设置Cookie

参考文档:http://magpcss.org/ceforum/apidocs3/projects/(default)/CefCookieManager.html

转载:https://www.cnblogs.com/guolixiucai/p/6994559.html

转载:https://blog.csdn.net/wangshubo1989/article/details/50520370

转载:https://blog.csdn.net/lee353086/article/details/42970909

转载:http://www.itboth.com/d/qyAB7j/cookie-cef-c++

Cookie是什么:

简单地说,cookie 就是浏览器储存在用户电脑上的一小段文本文件。cookie 是纯文本格式,不包含任何可执行的代码。一个 Web 页面或服务器告知浏览器按照一定规范来储存这些信息,并在随后的请求中将这些信息发送至服务器,Web 服务器就可以使用这些信息来识别不同的用户。大多数需要登录

的网站在用户验证成功之后都会设置一个 cookie,只要这个 cookie 存在并可以,用户就可以自由浏览这个网站的任意页面。再次说明,cookie 只包含数据,就其本身而言并不有害。

设置Cookie的失效时间:

如果Cookie没有设置expires属性,那么 cookie 的生命周期只是在当前的会话中,

关闭浏览器意味着这次会话的结束,此时 cookie 随之失效。

CEF3中,CefCookieManager这个类就是用来管理cookies的。

在头文件cef_cookies中,在cef_cookies_capi.h里,有详细的注释,和上边链接里的文档说明一样。

Cookies的管理无外乎Cookies的设置、获取、删除、查找,外加一个存储位置的设置。

Method Summary
 static CefRefPtr< CefCookieManager > CreateManager( const CefString& path, bool persist_session_cookies, CefRefPtrCefCompletionCallback > callback ) 
          Creates a new cookie manager.
 virtual bool DeleteCookies( const CefString& url, const CefString& cookie_name, CefRefPtrCefDeleteCookiesCallback > callback )= 0 
          Delete all cookies that match the specified parameters.
 virtual bool FlushStoreCefRefPtrCefCompletionCallback > callback )= 0 
          Flush the backing store (if any) to disk.
 static CefRefPtr< CefCookieManager > GetGlobalManagerCefRefPtrCefCompletionCallback > callback ) 
          Returns the global cookie manager.
 virtual bool SetCookie( const CefString& url, const CefCookie& cookie, CefRefPtrCefSetCookieCallback > callback )= 0 
          Sets a cookie given a valid URL and explicit user-provided cookie attributes.
 virtual bool SetStoragePath( const CefString& path, bool persist_session_cookies, CefRefPtrCefCompletionCallback > callback )= 0 
          Sets the directory path that will be used for storing cookie data.
 virtual void SetSupportedSchemes( const std::vector< CefString >& schemes, CefRefPtrCefCompletionCallback > callback )= 0 
          Set the schemes supported by this manager.
 virtual bool VisitAllCookiesCefRefPtrCefCookieVisitor > visitor )= 0 
          Visit all cookies on the IO thread.
 virtual bool VisitUrlCookies( const CefString& url, bool includeHttpOnly, CefRefPtrCefCookieVisitor > visitor )= 0 
          Visit a subset of cookies on the IO thread.
    std::wstring username_key = L"username";
    std::wstring username_value = L"chechen";
    std::wstring domain = L"www.cnblogs.com/chechen"

    CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager();
    CefCookie cookie;
    CefString(&cookie.name).FromWString(username_key.c_str());
    CefString(&cookie.value).FromWString(username_value.c_str());
    CefString(&cookie.domain).FromWString(domain.c_str());
    CefString(&cookie.path).FromASCII("/");
    cookie.has_expires = true;//设置Cookie时间
    cookie.expires.year = 2200;
    cookie.expires.month = 4;
    cookie.expires.day_of_week = 5;
    cookie.expires.day_of_month = 11;

    domain = L"https://" + domain;
    CefPostTask(TID_IO, NewCefRunnableMethod(manager.get(), &CefCookieManager::SetCookie,CefString(domain.c_str()), cookie));

注意:cookie.domain是不带”https://”的,而CefString(domain.c_str())中的domain是带”https://“的,一定要注意。

 

posted @ 2019-01-18 10:47  车臣  阅读(5874)  评论(0编辑  收藏  举报