QT 内嵌CEF 设置cookie

#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,只需要在网站注销就可以了。


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值