Results 1 to 3 of 3

Thread: [QNetworkAccessManager + QNetworkCookieJar] Cookies are not saved

  1. #1
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question [QNetworkAccessManager + QNetworkCookieJar] Cookies are not saved

    Hello!
    Objective: To load different pages of one site, with the ability to use previously issued cookie

    The simplest application loads the page from localhost.

    Qt Code:
    1. #include <QtCore>
    2. #include <QApplication>
    3. #include <QDebug>
    4. #include <QtNetwork>
    5.  
    6. class MyCookieJar : public QNetworkCookieJar
    7. {
    8. public:
    9. QList<QNetworkCookie> getAllCookies() { return allCookies(); }
    10. };
    11.  
    12.  
    13. class CookiesTest : public QObject
    14. {
    15. Q_OBJECT
    16. public:
    17. CookiesTest(QWidget* parent = 0);
    18.  
    19. private slots:
    20. void replyFinished(QNetworkReply*);
    21.  
    22. private:
    23. QNetworkAccessManager* manager;
    24. MyCookieJar *cookieJar;
    25. };
    26.  
    27. CookiesTest::CookiesTest(QWidget* parent)
    28. {
    29. manager = new QNetworkAccessManager;
    30. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    31.  
    32. cookieJar = new MyCookieJar;
    33. manager->setCookieJar(cookieJar);
    34.  
    35. manager->get(QNetworkRequest(QUrl("http://test1.ru/test/cookie.php")));
    36. manager->get(QNetworkRequest(QUrl("http://test1.ru/test/cookie.php")));
    37. }
    38.  
    39. void CookiesTest::replyFinished(QNetworkReply* reply)
    40. {
    41. qDebug() << reply->readAll();
    42. qDebug() << "getAllCookies: " << cookieJar->getAllCookies();
    43. }
    44.  
    45.  
    46. int main(int argc, char* argv[])
    47. {
    48. QApplication app(argc, argv);
    49. CookiesTest cookiesTest;
    50. return app.exec();
    51. }
    52.  
    53. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    in the file .pro: QT += network

    File cookie.php shows number of visits that page on the analysis of the cookie
    Qt Code:
    1. <?php
    2. if (isset($_COOKIE['Mortal'])) $cnt = $_COOKIE['Mortal'] + 1;
    3. else $cnt = 1;
    4.  
    5. setcookie("Mortal",$cnt,0x6FFFFFFF);
    6. echo "NUM: [".@$_COOKIE['Mortal']."]";
    7. ?>
    To copy to clipboard, switch view to plain text mode 
    Output:
    "NUM: []"
    getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
    "NUM: []"
    getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
    Although to be the second time in brackets the number 1 and the second getAllCookies: Mortal = 2

    Tell me please, where was the mistake, what's wrong? Why cookies are not stored on the 2nd request

  2. #2
    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: [QNetworkAccessManager + QNetworkCookieJar] Cookies are not saved

    At the time you issue your two asynchronous network requests the cookie jar is empty. Both requests go to the server without a cookie and come back with the same Mortal=1 value. The PHP end is working correctly.

    This version waits three seconds before issuing the second request:
    Qt Code:
    1. #include <QtCore>
    2. #include <QApplication>
    3. #include <QDebug>
    4. #include <QtNetwork>
    5.  
    6. class MyCookieJar : public QNetworkCookieJar
    7. {
    8. public:
    9. QList<QNetworkCookie> getAllCookies() { return allCookies(); }
    10. };
    11.  
    12.  
    13. class CookiesTest : public QObject
    14. {
    15. Q_OBJECT
    16. public:
    17. CookiesTest(QWidget* parent = 0);
    18.  
    19. private slots:
    20. void replyFinished(QNetworkReply*);
    21. void doAnother();
    22.  
    23. private:
    24. QNetworkAccessManager* manager;
    25. MyCookieJar *cookieJar;
    26. };
    27.  
    28. CookiesTest::CookiesTest(QWidget* parent)
    29. {
    30. manager = new QNetworkAccessManager;
    31. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    32.  
    33. cookieJar = new MyCookieJar;
    34. manager->setCookieJar(cookieJar);
    35.  
    36. manager->get(QNetworkRequest(QUrl("http://ptolemy/test.php")));
    37. QTimer::singleShot(3000, this, SLOT(doAnother()));
    38. }
    39.  
    40. void CookiesTest::replyFinished(QNetworkReply* reply)
    41. {
    42. qDebug() << reply->readAll();
    43. qDebug() << "getAllCookies: " << cookieJar->getAllCookies();
    44. }
    45.  
    46. void CookiesTest::doAnother() {
    47. manager->get(QNetworkRequest(QUrl("http://ptolemy/test.php")));
    48. }
    49.  
    50. int main(int argc, char* argv[])
    51. {
    52. QApplication app(argc, argv);
    53. CookiesTest cookiesTest;
    54. return app.exec();
    55. }
    56.  
    57. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 1st March 2012 at 00:10.

  3. #3
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QNetworkAccessManager + QNetworkCookieJar] Cookies are not saved

    Thank you very much ChrisW67! It works.

    Perhaps I still misunderstand something, but that's an option for some reason just does not work, cookies Mortal = 1
    Qt Code:
    1. #include <QtCore>
    2. #include <QApplication>
    3. #include <QDebug>
    4. #include <QtNetwork>
    5.  
    6. class MyCookieJar : public QNetworkCookieJar
    7. {
    8. public:
    9. QList<QNetworkCookie> getAllCookies() { return allCookies(); }
    10. };
    11.  
    12.  
    13. class CookiesTest : public QObject
    14. {
    15. Q_OBJECT
    16. public:
    17. CookiesTest(QWidget* parent = 0);
    18. void download(QString);
    19.  
    20. private slots:
    21. void replyFinished(QNetworkReply*);
    22. void doAnother();
    23.  
    24. private:
    25. QNetworkAccessManager* manager;
    26. MyCookieJar *cookieJar;
    27. QUrl myUrl;
    28. };
    29.  
    30. CookiesTest::CookiesTest(QWidget* parent)
    31. {
    32. manager = new QNetworkAccessManager;
    33. cookieJar = new MyCookieJar;
    34. manager->setCookieJar(cookieJar);
    35.  
    36. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    37. }
    38.  
    39. void CookiesTest::replyFinished(QNetworkReply* reply)
    40. {
    41. qDebug() << reply->readAll();
    42. qDebug() << "getAllCookies: " << cookieJar->getAllCookies();
    43. }
    44.  
    45. void CookiesTest::doAnother() {
    46. manager->get(QNetworkRequest(myUrl));
    47. }
    48.  
    49. void CookiesTest::download(QString url)
    50. {
    51. myUrl = QUrl(url);
    52. QTimer::singleShot(500, this, SLOT(doAnother()));
    53. }
    54.  
    55. int main(int argc, char* argv[])
    56. {
    57. QApplication app(argc, argv);
    58. CookiesTest cookiesTest;
    59. cookiesTest.download("http://test1.ru/test/cookie.php");
    60. cookiesTest.download("http://test1.ru/test/cookie.php");
    61. return app.exec();
    62. }
    63.  
    64. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Output:
    "NUM: []"
    getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
    "NUM: []"
    getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )

    But I found another solution to this

    Qt Code:
    1. #include <QtCore>
    2. #include <QApplication>
    3. #include <QDebug>
    4. #include <QtNetwork>
    5.  
    6.  
    7. class MyCookieJar : public QNetworkCookieJar
    8. {
    9. public:
    10. QList<QNetworkCookie> getAllCookies() { return allCookies(); }
    11. };
    12.  
    13.  
    14. class CookiesTest : public QObject
    15. {
    16. Q_OBJECT
    17. public:
    18. CookiesTest(QObject *parent = 0);
    19. void download(QString);
    20. void _download(QUrl);
    21.  
    22. private:
    23. QNetworkAccessManager* manager;
    24. MyCookieJar *cookieJar;
    25. QList<QNetworkCookie> cookiesList;
    26. };
    27.  
    28. CookiesTest::CookiesTest(QObject *parent) : QObject(parent)
    29. {
    30. manager = new QNetworkAccessManager;
    31. cookieJar = new MyCookieJar();
    32. manager->setCookieJar(cookieJar);
    33. }
    34.  
    35. void CookiesTest::download(QString url)
    36. {
    37. _download(QUrl(url));
    38. }
    39.  
    40. void CookiesTest::_download(QUrl url)
    41. {
    42. //enters event loop, simulate blocking io
    43. QTimer t;
    44. t.setSingleShot(true);
    45. connect(&t, SIGNAL(timeout()), &q, SLOT(quit()));
    46.  
    47. QNetworkReply *reply = manager->get(QNetworkRequest(url));
    48. connect(reply, SIGNAL(finished()), &q, SLOT(quit()));
    49.  
    50. t.start(5000);
    51. q.exec();
    52.  
    53. if (t.isActive()) {
    54. t.stop();
    55. QByteArray response = reply->readAll();
    56. qDebug() << response;
    57.  
    58. QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    59. if (reply->error()) {
    60. qDebug()<< "Download failed" << reply->errorString();
    61. } else if (!redirectionTarget.isNull()) {
    62. QUrl newUrl = url.resolved(redirectionTarget.toUrl());
    63. qDebug()<< "Redirect to" << newUrl.toString();
    64. url = newUrl;
    65. // reply->deleteLater();
    66. _download(url);
    67. } else {
    68. qDebug() << "Finish! ";
    69. }
    70.  
    71. reply->deleteLater();
    72.  
    73. } else {
    74. qDebug() << "Timeout";
    75. }
    76.  
    77. cookiesList = cookieJar->getAllCookies();
    78. qDebug() << "getAllCookies: " << cookiesList;
    79. }
    80.  
    81. int main(int argc, char* argv[])
    82. {
    83. QApplication app(argc, argv);
    84. CookiesTest cookiesTest;
    85. cookiesTest.download("http://test1.ru/test/cookie.php");
    86. cookiesTest.download("http://test1.ru/test/cookie.php");
    87. cookiesTest.download("http://test1.ru/test/cookie.php");
    88. return app.exec();
    89. }
    90.  
    91. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Output:
    "NUM: []"
    Finish!
    getAllCookies: (QNetworkCookie("Mortal=1; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
    "NUM: [1]"
    Finish!
    getAllCookies: (QNetworkCookie("Mortal=2; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )
    "NUM: [2]"
    Finish!
    getAllCookies: (QNetworkCookie("Mortal=3; expires=Wed, 18-Jul-2029 05:49:51 GMT; domain=test1.ru; path=/test/") )

Similar Threads

  1. Replies: 0
    Last Post: 31st October 2011, 13:33
  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. QNetworkAccessManager can't handle multiple cookies?
    By krippy2k in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2009, 00:12
  5. Replies: 1
    Last Post: 17th February 2009, 17:55

Tags for this Thread

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.