Results 1 to 12 of 12

Thread: QNetworkCookieJar

  1. #1
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default QNetworkCookieJar

    So i set a QnetworkCookieJar for my QNetworkAccessManager

    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 

    and in Qt Documentation :
    In order to implement your own security policy, override the QNetworkCookieJar::cookiesForUrl() and QNetworkCookieJar::setCookiesFromUrl() virtual functions. Those functions are called by QNetworkAccessManager when it detects a new cookie.
    What if I don't want to implement my own security policy or whatever, do I need to implement my own setCookiesFromUrl ?
    Don't I need to specify the location to store cookies? 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?

    Using QFile?

    Networkjar.h
    Qt Code:
    1. #ifndef NETWORKJAR_H
    2. #define NETWORKJAR_H
    3.  
    4. #include <QNetworkCookieJar>
    5.  
    6. class NetworkJar: public QNetworkCookieJar
    7. {
    8.  
    9. public:
    10. NetworkJar();
    11. bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
    12. QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
    13. };
    14.  
    15. #endif // NETWORKJAR_H
    To copy to clipboard, switch view to plain text mode 

    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. }
    26.  
    27. QList<QNetworkCookie> NetworkJar::cookiesForUrl(const QUrl &url) const
    28. {
    29. //Later
    30. }
    To copy to clipboard, switch view to plain text mode 

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

    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,
    _

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

    ttimt (26th February 2014)

  4. #3
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkCookieJar

    Quote Originally Posted by anda_skoa View Post
    Or using explicit save/load methods that get called at the end/start of the program.
    What's that


    Quote Originally Posted by anda_skoa View Post
    That seems to ignore the "url" argument, which will likely make it difficult to then later retrieve the cookies for that URL.
    I haven't implement cookiesforURL. so I'll output the URL to my file too.





    Those functions are called by QNetworkAccessManager when it detects a new cookie.
    Tested. And I think it won't load, my cookies are saved in a file through setcookiesfromurl function. But when I create a new QNetworkAccessManager and access the same web, the Qnetworkaccessmanager just reset my files. Tried create a function in networkjar class and run cookiesforUrl then set it using this->setallcookies. still the same
    *EDIT : Maybe I'll need to check the cookie existence before writing and append new data not rewrite Will do that soon



    EDIT: Removed
    Last edited by ttimt; 26th February 2014 at 19:28.

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

    Default Re: QNetworkCookieJar

    Quote Originally Posted by ttimt View Post
    What's that
    A method?
    This usually refers to a function declared as part of a class's interface.

    Cheers,
    _

  6. #5
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkCookieJar

    Quote Originally Posted by anda_skoa View Post
    A method?
    This usually refers to a function declared as part of a class's interface.

    Cheers,
    _
    The Save/Load



    I tried to check for cookie name before writing to file but this is outputting nothing?

    Qt Code:
    1. //Setup
    2. QList<QNetworkCookie> cookies = QNetworkCookieJar::allCookies();
    3. QFile cookiesfile("cookiefile");
    4. QTextStream in(&cookiesfile);
    5. QList<QString> savedcookies;
    6.  
    7. //Read
    8. if (!cookiesfile.open(QFile::ReadOnly | QFile::Text))
    9. {
    10. emit networkreplyError("Failed to open file for reading!");
    11. }
    12. while (!cookiesfile.atEnd())
    13. {
    14. savedcookies << in.readLine();
    15. }
    16. in.flush();
    17. cookiesfile.close();
    18.  
    19. //Append
    20. QTextStream out(&cookiesfile);
    21. if (cookiesfile.open(QFile::Append | QFile::Text))
    22. {
    23. emit networkreplyError("Failed to open file for reading");
    24. }
    25.  
    26. foreach (QNetworkCookie cookie, cookieList)
    27. {
    28. for (int i = savedcookies.size()-1; i >= 0; i--)
    29. {//will add check URL later
    30. if (!savedcookies.at(i).contains(cookie.name()))
    31. {
    32. //Name, Value, Path, Domain, Expire Date, URL
    33. out << "Name:" << cookie.name() << " spliter " << "Value:" << cookie.value() << " spliter " << "Path:" << cookie.path() << " spliter " << "Domain:" << cookie.domain() << " spliter " << "Expire Date:" << cookie.expirationDate().toString() << " spliter " << "URL:" << url.toString() << endl;
    34. out.flush();
    35. cookies += cookie;
    36. }
    37. }
    38. }
    39.  
    40. //Close and Return
    41. cookiesfile.close();
    42. return true;
    To copy to clipboard, switch view to plain text mode 
    Last edited by ttimt; 27th February 2014 at 12:43.

  7. #6
    Join Date
    Feb 2014
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkCookieJar

    Hello, everyone

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

    Default Re: QNetworkCookieJar

    Quote Originally Posted by ttimt View Post
    The Save/Load
    Oh, I thought that would be self explanatory :-)
    A save method would write the cookies into a file and a load method would read them from a file.

    Quote Originally Posted by ttimt View Post
    I tried to check for cookie name before writing to file but this is outputting nothing?
    I am not quite sure what you are trying to do here and where in the class you are doing it.
    Trying to avoid duplicates in setCookiesForUrl()?

    That would be another thing that is easier in an explicit save method, because there you can just overwrite the file with the current jar content.

    Cheers,
    _

  9. #8
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkCookieJar

    I mean what save/load is that, that get "called at the end/start" ?
    Maybe I misinterpreted this.
    You probably saying create functions that get called at the end/start, not some special qt save/load function that is not created by me


    I'm in NetworkJar class, subclass QNetworkCookieJar.
    That function is setCookiesforUrl, trying to check if cookies already exists before writing.
    Last edited by ttimt; 27th February 2014 at 14:08.

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

    Default Re: QNetworkCookieJar

    Quote Originally Posted by ttimt View Post
    I mean what save/load is that, that get "called at the end/start" ?
    Maybe I misinterpreted this.
    You probably saying create functions that get called at the end/start, not some special qt save/load function that is not created by me
    Exactly. I was suggeting to use explicit methods for saving and loading instead of making the setter and getter do it on every invocation.

    Cheers,
    _

  11. #10
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkCookieJar

    Do I need to use setallcookies if I returned a Qlist<QNetworkCookie> in cookiesforurl ???
    I'm a newbie. Don't trust me

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

    Default Re: QNetworkCookieJar

    I doubt it. setAllCookies() sounds more like the function you would call in a load method, i.e. to initially fill the cookie jar.

    Cheers,
    _

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

    ttimt (2nd March 2014)

  14. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkCookieJar

    The Qt browser demo implements cookie persistence. See cookiejar.h and cpp.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.