QNetworkCookieJar - Retrieving individual data from cookie



  • I have set up a standard QNetworkCookieJar to be used with multiple QWebView I use in my application.

    I need to retrieve some cookie data in order to create a custom c++ object
    However the function allCookie() is protected and I can't seem to get them.

    Anyone did this before?

    Here is my code trying to retrieve the cookies :
    @ QNetworkAccessManager nam = qApp->property("NetworkManager").value<QNetworkAccessManager>();
    QNetworkCookieJar *jar = nam->cookieJar();

    QList<QNetworkCookie> cookiesLst = jar->allCookies();
     for (int i = cookiesLst.count() - 1; i >= 0; --i) {
            qDebug() << "found cookie here";
     }@
    

    Thanks

    C:\Dropbox\PowerVelo2\gui\mainwindow.cpp:47: 'QNetworkCookieJar::allCookies' : cannot access protected member declared in class 'QNetworkCookieJar'



  • You can use "QNetworkCookieJar::cookiesForUrl":http://qt-project.org/doc/qt-5/qnetworkcookiejar.html#cookiesForUrl

    Or you can derive your class from QNetworkCookieJar and use allCookies() in that class as it is "suggested":http://qt-project.org/doc/qt-5/qnetworkcookiejar.html#allCookies in the doc.


  • Lifetime Qt Champion

    Hi,

    You can't access protect functions unless you are deriving from that class. From a quick look at QNetworkCookieJar, you'll have to use cookiesForUrl



  • Thanks for the guidance guys.
    cookiesForUrl is perfect for my needs, I don't need to persist the cookie for now.

    I'm using codeIgniter to set the cookie on the server side :
    http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

    I retrieve the cookie this way:
    @ QList<QNetworkCookie> cookiesLst = jar->cookiesForUrl(QUrl(Environnement::getURLEnvironnement()));

    foreach (QNetworkCookie cookie, cookiesLst) {
        qDebug() << cookie.name();
        qDebug() << cookie;
    }@
    

    log :
    @"ci_session"
    QNetworkCookie("ci_session=a:4:{s:10:"session_id";s:32:"ee434b8cba49682794ec50eeee959917";s:10:"ip_address";s:3:"::1";s:10:"user_agent";s:101:"Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/538.1+(KHTML,+like+Gecko)+MaximumTrainer+Safari/538.1";s:13:"last_activity";i:1405003057;}0aab4290e254a90ca848c80362216400; expires=Sat, 09-Jul-2016 14:37:50 GMT; domain=localhost; path=/")
    "localhost" @

    I'll have to find how to retrieve custom session data, so far it only return me the basic codeIgniter cookie but not the data I added. if it's too complex i'll just make a request to a webservice instead at the cost of one more DB requets.
    Thanks for the help guys


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.