Results 1 to 12 of 12

Thread: QNetworkCookieJar

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: QNetworkCookieJar

    Quote Originally Posted by ttimt View Post
    Qt Code:
    1. //Setup QNetwork
    2. loginnam = new QNetworkAccessManager();
    3.  
    4. //Cookies
    5. NetworkJar njarobj;
    6. loginnam->setCookieJar(&njarobj);
    7. njarobj.setParent(0);
    To copy to clipboard, switch view to plain text mode 
    That is unlikely to work properly, njarobj is on the stack and will go out of scope at then end of the block (probably a function body).

    Quote Originally Posted by ttimt View Post
    What if I don't want to implement my own security policy or whatever, do I need to implement my own setCookiesFromUrl ?
    You don't need a derived class if you don't want to change anything that QNetworkCookieJar does

    Quote Originally Posted by ttimt View Post
    Don't I need to specify the location to store cookies?
    QNetworkCookieJar does not persist cookies. Any derived class that implements persistence needs to address this depending on the persistence mechanism chosen for its implementation.


    Quote Originally Posted by ttimt View Post
    But I don't see that function, so I assume I'll need to reimplement SetCookiesFromUrl to store to disk and CookiesFromUrl to get from disk?
    Or using explicit save/load methods that get called at the end/start of the program.

    Quote Originally Posted by ttimt View Post
    Using QFile?
    Indeed a valid option.


    Quote Originally Posted by ttimt View Post
    Networkjar.cpp
    Qt Code:
    1. #include "networkjar.h"
    2. #include <QFile>
    3. #include <QDateTime>
    4. #include <QTextStream>
    5.  
    6. NetworkJar::NetworkJar()
    7. {
    8. }
    9. bool NetworkJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
    10. {
    11. QList<QNetworkCookie> cookies = QNetworkCookieJar::allCookies();
    12.  
    13. QFile cookiesfile("Cookies");
    14. QTextStream out(&cookiesfile);
    15. if (!cookiesfile.open(QFile::WriteOnly | QFile::Text))
    16. {
    17. return false;
    18. }
    19. foreach (QNetworkCookie cookie, cookieList)
    20. {
    21. cookies += cookie;
    22. out << "Name:" << cookie.name() << "Value:" << cookie.value() << "Path:" << cookie.path() << "Expire Date:" << cookie.expirationDate().toString();
    23. }
    24. return true;
    25. }
    To copy to clipboard, switch view to plain text mode 
    That seems to ignore the "url" argument, which will likely make it difficult to then later retrieve the cookies for that URL.

    Cheers,
    _

  2. The following user says thank you to anda_skoa for this useful post:

    ttimt (26th February 2014)

Similar Threads

  1. Replies: 2
    Last Post: 1st March 2012, 11:59
  2. QWebView and set QNetworkCookieJar
    By Talei in forum Newbie
    Replies: 0
    Last Post: 10th June 2010, 07:20
  3. Replies: 11
    Last Post: 12th July 2009, 17:01
  4. Replies: 1
    Last Post: 17th February 2009, 17:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.