How do I save cookies with Qt?

前端 未结 3 775
臣服心动
臣服心动 2021-01-01 05:20

I am trying to save cookies that are produced by my app to disk location such as C:\\Users\\Username\\AppData\\Local\\MyCompany\\MyApp. I have implemented a web

相关标签:
3条回答
  • Base of qt example, http://qt.gitorious.org/qt/qt/trees/4.8/demos/browser, i wrote this class that save and use one cookie for me. Perhaps it helps you too. Note that it just save one cookie and not list of cookies.

    #include "cookiejar.h"
    
    
    CookieJar::CookieJar(QObject *parent)
        : QNetworkCookieJar(parent)
        , m_loaded(false)
    {
    }
    
    void CookieJar::load()
    {
        if (m_loaded)
            return;
    
        QSettings settings;
        settings.beginGroup(QLatin1String("cookies"));
        QList<QNetworkCookie> savedCookies = QNetworkCookie::parseCookies(settings.value("cookies").toByteArray());
    
        for (int j = 0; j < savedCookies.count(); j++)
            insertCookie(savedCookies.at(j));
    
        m_loaded = true;
        emit cookiesChanged();
    }
    
    void CookieJar::save()
    {
        if (!m_loaded)
            return;
    
        QList<QNetworkCookie> cookies = allCookies();
    
        QSettings settings;
        settings.beginGroup(QLatin1String("cookies"));
        settings.setValue("cookies", cookies[0].toRawForm());
    }
    
    QList<QNetworkCookie> CookieJar::cookiesForUrl(const QUrl &url) const
    {
        // This function is called by the default QNetworkAccessManager::createRequest(),
        // which adds the cookies returned by this function to the request being sent.
    
        CookieJar *that = const_cast<CookieJar*>(this);
        if (!m_loaded)
            that->load();
    
        return QNetworkCookieJar::cookiesForUrl(url);
    }
    
    bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
    {
        if (!m_loaded)
            load();
    
        QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
        save();     //Save cookie permanently in setting file.
        emit cookiesChanged();
        return true;
    }
    
    0 讨论(0)
  • 2021-01-01 05:49

    You will need to subclass QNetworkCookieJar and in that class you should implement your own persistent storage.

    class MyNetworkCookieJar : public QNetworkCookieJar {
    
    public: 
    
    bool saveCookiesToDisk() {
    // .. my implementation
    return true; // if i did
    }
    
    bool loadCookiesFromDisk() {
    // .. load from disk
    return false; // if unable to.
    }
    }
    

    The sample application from Qt project implements a persistent cookie store, it could be a good starting point for you: http://qt.gitorious.org/qt/qt/trees/4.8/demos/browser

    look at cookiejar.h and cookiejar.cpp

    0 讨论(0)
  • 2021-01-01 05:52

    @Musa's answer is good but it only saves one cookie. I recommend using the Qt folk's implementation from the old qmlviewer located here: http://code.qt.io/cgit/qt/qt.git/tree/tools/qml/qmlruntime.cpp?h=4.7#n438

    Here's the code:

    class PersistentCookieJar : public QNetworkCookieJar {
    public:
        PersistentCookieJar(QObject *parent) : QNetworkCookieJar(parent) { load(); }
        ~PersistentCookieJar() { save(); }
    
        virtual QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const
        {
            QMutexLocker lock(&mutex);
            return QNetworkCookieJar::cookiesForUrl(url);
        }
    
        virtual bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
        {
            QMutexLocker lock(&mutex);
            return QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
        }
    
    private:
        void save()
        {
            QMutexLocker lock(&mutex);
            QList<QNetworkCookie> list = allCookies();
            QByteArray data;
            foreach (QNetworkCookie cookie, list) {
                if (!cookie.isSessionCookie()) {
                    data.append(cookie.toRawForm());
                    data.append("\n");
                }
            }
            QSettings settings;
            settings.setValue("Cookies",data);
        }
    
        void load()
        {
            QMutexLocker lock(&mutex);
            QSettings settings;
            QByteArray data = settings.value("Cookies").toByteArray();
            setAllCookies(QNetworkCookie::parseCookies(data));
        }
    
        mutable QMutex mutex;
    };
    
    0 讨论(0)
提交回复
热议问题