How to set QNetworkCookieJar in QWebEngine?

百般思念 提交于 2019-12-05 05:24:37

问题


In QWebView it was possible to set a QNetworkCookieJar via QNetworkAccessManager.

QNetworkAccessManager *nam = new QNetworkAccessManager();    
nam->setCookieJar(cookieJar);
webView->page()->setNetworkAccessManager(nam);    

This was working like a charm.

How can I set a QNetworkCookieJar in new QWebEngine class introduced in Qt5.4?


回答1:


I found a solution, to share Cookies with QWebEngine and QNetworkAccesManager by using QWebEngineCookieStore. Subclass QNetworkCookieJar:

class CookieWebEngine : public QNetworkCookieJar
{
.....
protected:
    // Reimplement this functions to work with your _cookies list;
    bool insertCookie(const QNetworkCookie &cookie);
    bool deleteCookie(const QNetworkCookie &cookie);
    bool updateCookie(const QNetworkCookie &cookie);
    bool validateCookie(const QNetworkCookie &cookie, const QUrl &url) const;
private:
    // Save Chromium Cookies
    QWebEngineCookieStore *_store;
    // Save available cookies
    QList<QNetworkCookie> _cookies;
}

Now, lets implement a function to load/save cookies in a file:

// Load Chromium Cookies
void CookieWebEngine::loadStore() {
    // Save cookies profile shared
    QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
    _store = WebEngineProfile::defaultProfile()->cookieStore();
    connect(_store, &QWebEngineCookieStore::cookieAdded, this, &CookieWebEngine::handleCookieAdded);
    _store->loadAllCookies();
}
// Load/Save cookies in arraylist in a file
void CookieWebEngine::load() {

    // load cookies and exceptions
    qRegisterMetaTypeStreamOperators<QList<QNetworkCookie> >("QList<QNetworkCookie>");
    const QString location = cookiesDirectory() + COOKIES_FILE;
    QSettings cookieSettings(location, QSettings::IniFormat);
    _cookies = qvariant_cast<QList<QNetworkCookie> >(cookieSettings.value(QLatin1String("cookies")));
    setAllCookies(_cookies);
    // Now user iterate and add it to chromium
    for (auto cookie : _cookies) {
       _store->setCookie(cookie);
    }
    cookieSettings.sync();
}

void CookieWebEngine::save()
{
    QString directory = cookiesDirectory();
    if (!QFile::exists(directory)) {
        QDir dir;
        dir.mkpath(directory);
    }
    const QString location = directory + COOKIES_FILE;
    QSettings cookieSettings(location, QSettings::IniFormat);
    cookieSettings.setValue(QLatin1String("cookies"), QVariant::fromValue<QList<QNetworkCookie>>(_cookies));
    cookieSettings.sync();
}

Now, just connect and handle cookies loaded from the webview:

void CookieWebEngine::handleCookieAdded(const QNetworkCookie &cookie)
{
    if (insertCookie(cookie)) {
        qDebug() << "Handle cookie " << cookie;
    }
}

Its working well for me. Now, i use chromium to sign in. After, i save my session cookie in the customized cookiejar and I use it in my customized QNetworkAccesManager.



来源:https://stackoverflow.com/questions/27430864/how-to-set-qnetworkcookiejar-in-qwebengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!