Results 1 to 12 of 12

Thread: QNetworkCookieJar::allCookies() const is protected

  1. #1
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QNetworkCookieJar::allCookies() const is protected

    Hi all,
    this is my first post on the forum and in these days are started my journeys in c++ and qt4.

    My problem is with this line of code:

    Qt Code:
    1. QList<QNetworkCookie> cookiesList(cookieJar->allCookies());
    To copy to clipboard, switch view to plain text mode 

    which gives the following error during make:

    /usr/include/qt4/QtNetwork/qnetworkcookie.h: In member function ‘void HttpWindow::replyFinished(QNetworkReply*)’:
    /usr/include/qt4/QtNetwork/qnetworkcookie.h:121: error: ‘QList<QNetworkCookie> QNetworkCookieJar::allCookies() const’ is protected
    Maybe this is c++ matter than qt4; since QNetworkCookieJar::allCookies() is protected how assign its value to my cookiesList?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkCookieJar::allCookies() const is protected

    You have to use QNetworkCookieJar::setCookiesFromUrl(). The method you mentioned is useful if you're implementing your own cookie jar and want to save cookies across sessions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Probably my first post was incomplete.

    From the beginning: my application needs to login in a site and execute some opertations; so I have to store cookies:

    Qt Code:
    1. manager = new QNetworkAccessManager;
    2. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    3.  
    4. cookieJar = new QNetworkCookieJar;
    5. manager->setCookieJar(cookieJar);
    To copy to clipboard, switch view to plain text mode 

    I thought that once called manager->post(...) all cookies from website were stored in cookieJar so with cookieJar->allCookies() I could check them and, yes, reuse cookieJar for next operations in the site.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkCookieJar::allCookies() const is protected

    You have to store them yourself in the cookie jar, as far as I remember.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Quote Originally Posted by wysota View Post
    You have to store them yourself in the cookie jar, as far as I remember.
    And from where, since QNetworkReply seems not have any function related to cookies. Sorry wysota, but network argument is very tough in the documentation.

    thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Quote Originally Posted by svello View Post
    And from where, since QNetworkReply seems not have any function related to cookies.
    Sure it does - QNetworkReply::header() with QNetworkRequest::SetCookieHeader.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Quote Originally Posted by wysota View Post
    Sure it does - QNetworkReply::header() with QNetworkRequest::SetCookieHeader.
    Now the only problem is converting a QVariant in QList<QNetworkCookie>.

    I've tryed this

    Qt Code:
    1. QList<QNetworkCookie> cookiesList(reply->header(QNetworkRequest::SetCookieHeader).toList());
    To copy to clipboard, switch view to plain text mode 

    with no luck. It creates a QList<QVariant> and can't be used for cookiesList. Any tips?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Qt Code:
    1. QList<QNetworkCookie> cookiesList = qvariant_cast<QList<QNetworkCookie> >(reply->header(...));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. QList<QNetworkCookie> cookiesList = qvariant_cast<QList<QNetworkCookie> >(reply->header(...));
    To copy to clipboard, switch view to plain text mode 
    Thanks wysota, now it compiles correctly but as result I got an empty cookiesList; it is how no cookies were sent by the website. I have made a minimal example for testing purpose; have added a webview for checking site message and it says "Cookies are not enabled!":

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QtNetwork>
    4. #include <QtWebKit>
    5.  
    6. class CookiesTest : public QDialog
    7. {
    8. Q_OBJECT
    9. public:
    10. CookiesTest(QWidget* parent = 0);
    11.  
    12. private slots:
    13. void replyFinished(QNetworkReply*);
    14.  
    15. private:
    16. QWebView* webView;
    17. QLabel* label;
    18. QNetworkAccessManager* manager;
    19. };
    20.  
    21. CookiesTest::CookiesTest(QWidget* parent)
    22. : QDialog(parent)
    23. {
    24. resize(640,480);
    25.  
    26. webView = new QWebView; // web page error messages
    27. label = new QLabel; // displays num of cookies received
    28. QVBoxLayout* layout = new QVBoxLayout(this);
    29. layout->addWidget(webView);
    30. layout->addWidget(label);
    31.  
    32. manager = new QNetworkAccessManager;
    33. connect(manager, SIGNAL(finished(QNetworkReply*)),
    34. this, SLOT(replyFinished(QNetworkReply*)));
    35.  
    36. manager->get(QNetworkRequest(QUrl("http://www.tempesttech.com/cookies/cookietest2.asp")));
    37. }
    38.  
    39. void CookiesTest::replyFinished(QNetworkReply* reply)
    40. {
    41. qDebug() << "Num of cookies: "
    42. << reply->header(QNetworkRequest::SetCookieHeader).toList().size();
    43. label->setText("Num of cookies: "
    44. + QVariant(reply->header(QNetworkRequest::SetCookieHeader).toList().size()).toString());
    45. webView->setContent(reply->readAll());
    46. }
    47.  
    48.  
    49. int main(int argc, char* argv[])
    50. {
    51. QApplication app(argc, argv);
    52. CookiesTest cookiesTest;
    53. cookiesTest.exec();
    54. }
    55.  
    56. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Did the website send you any cookies? Can you verify that with a network sniffer?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    svello (12th July 2009)

  12. #11
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Quote Originally Posted by svello View Post
    Maybe this is c++ matter than qt4; since QNetworkCookieJar::allCookies() is protected
    Just for the record, to explain some basics of object orientation: Yes, this is a C++ matter. "protected" means that you can access this method only in classes derived from QNetworkCookieJar. "protected" methods are hidden, and thus, not accessible, from any other classes.

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

    svello (12th July 2009)

  14. #12
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QNetworkCookieJar::allCookies() const is protected

    Quote Originally Posted by wysota View Post
    Did the website send you any cookies? Can you verify that with a network sniffer?
    Ok, solved! The problem was the wrong url; the right url for testing cookie ability is

    http://www.tempesttech.com/cookies/cookietest1.asp

    this link sets the cookie while that in my previous post checks only if it exists.

    Many thanks, wysota.

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. shared vs static
    By alisami in forum Installation and Deployment
    Replies: 3
    Last Post: 4th October 2008, 13:04
  3. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 07:43
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 19:47

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.