#pragma once
#include <QObject>
#include "include/cef_client.h"
#include <qapplication.h>
class CustomCefRequestContextHandler : public QObject, public CefRequestContextHandler
{
Q_OBJECT
public:
CustomCefRequestContextHandler(QObject *parent);
~CustomCefRequestContextHandler();
protected:
CefRefPtr<CefCookieManager> GetCookieManager() OVERRIDE{
CefRefPtr<CefCookieManager> manager =
CefCookieManager::GetGlobalManager(nullptr);
DCHECK(manager.get());
CefCookie cookie;
CefString(&cookie.name).FromString("pifa_session");
CefString(&cookie.value).FromString("eyJpdiI6Ikl5bVhcL1wvYW9sM1FnQjU4eHBnTHV5dz09IiwidmFsdWUiOiJsVTBqbVp5UzhUV040ajZ6UjUxSSt3NFpvbDNtcTV1TVBZT3R1WkswMTFKTm91eW1HamlQQU5YcEtodkExeGtpdk1aTFBucmVQUjJGQjRXdDBHeXpFdz09IiwibWFjIjoiYWQwZWNhNGRkMTk1NmYxNGYzNmIyYjgwODA0MzU5NzlkMzZhMDk0YTliYTNlYzQzMTkwOGU0MTY0OWE0MTVlNyJ9");
CefString(&cookie.domain).FromString("oa.awotuan.com");
//CefString(&cookie.path).FromString("/");//eg:"www.baidu.com"
//cookie.has_expires = true;
//cookie.expires.year = 2018;
//cookie.expires.month = 7;
//cookie.expires.day_of_week = 5;
//cookie.expires.day_of_month = 28;
std::string url = "http://oa.awotuan.com";//eg:"http://www.baidu.com"
//manager->SetCookie(url, cookie, nullptr);
manager->SetStoragePath(QApplication::applicationDirPath().toStdString(), true, NULL);
return CefCookieManager::GetGlobalManager(NULL);
}
private:
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(CustomCefRequestContextHandler);
};
完成测试。
managetr->SetCookie 测试手动设置有效果,注意的是,url需要带http://标识,https://还不清楚怎么设置
如果只是使用cookie,一行就可以:
manager->SetStoragePath(QApplication::applicationDirPath().toStdString(), true, NULL);
再运行程序可以发现在安装目录有两个cookie数据库文件,程序启动的时候会自动加载。
清除cookies,只需要在网站注销就可以了。
本文介绍如何在C++项目中使用CefCookieManager来管理Cookie,包括手动设置Cookie的具体方法及注意事项,并展示了如何通过一行代码设置Cookie存储路径。
1374

被折叠的 条评论
为什么被折叠?



