Results 1 to 4 of 4

Thread: qNetworkAccessManager and cookies

  1. #1
    Join Date
    Jul 2021
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default qNetworkAccessManager and cookies

    I'm trying to download a website (youtube) that opens after user passes the consent page (accepting cookies). So i create QNetworkRequest request and set RawHeader to ("COOKIE" , "CONSENT=YES+42"). It works fine, but only with the first attempt to download. With every next attempt i bounce against the consent page. The problem is somehow bypassed when each time i use deleteLater() on QNetworkAccessManager object. But the documentation claims "One QNetworkAccessManager instance should be enough for the whole Qt application" (also creating new instance of QNetworkAccessManager for each use eventually results with not receiving a replay and rise of processor use). So my question is how to "reset" QNetworkAccessManager so with each next use, it acts as with the first request. My code looks like this:
    Qt Code:
    1. Youtube::Youtube(QObject *parent) : QObject(parent)
    2. {
    3. manager = new QNetworkAccessManager(this);
    4. }
    5.  
    6. void Youtube::makeRequest(QString indexCore){
    7. QNetworkReply *reply;
    8. QNetworkRequest request;
    9. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotReadyRead(QNetworkReply*)));
    10. request.setRawHeader("COOKIE" , "CONSENT=YES+42" ); //works
    11. request.setUrl(QUrl("https://" + indexCore ));
    12. reply = manager->get(request);
    13. }
    14.  
    15. void Youtube::slotReadyRead(QNetworkReply *replay)
    16. {
    17. QByteArray dataTemp = replay->readAll();
    18. website = dataTemp.toStdString();
    19. replay->deleteLater();
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,018
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: qNetworkAccessManager and cookies

    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotReadyRead(QNetworkReply*)));
    You are making a new signal-slot connection every time makeRequest() is executed. This means the same slot will be executed extra times for each signal sent by the manager. Not only that, but on subsequent invocations of the same slot as a result of that one signal, you will be using a potentially invalid QNetworkReply (because you have asked it to delete itself).

    Make the connection once, in the constructor where you create the QNetworkAccessManager instance.

    In addition, in line 12 you have a memory leak because you do not delete the QNetworkReply instance that is created in the get() call.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2021
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qNetworkAccessManager and cookies

    Thanks. Though the problem remains, that with the first query i receive the correct answer from Youtube, but with the 2nd i return to the consent page, which demands correct cookie. In the code below i achieve right answers all the time but only when i delete QNetworkCookieJar each time, which is not the right practice according the documentation, and that also may lead to runtime error.

    Qt Code:
    1. Youtube::Youtube(QObject *parent) : QObject(parent)
    2. {
    3. manager = new QNetworkAccessManager(this);
    4. cookieJar = new QNetworkCookieJar(this);
    5. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotReadyRead(QNetworkReply*)));
    6. }
    7.  
    8. void Youtube::makeRequest(QString indexCore)
    9. {
    10. QNetworkRequest request;
    11. QNetworkCookie cookie;
    12. cookie.setName("CONSENT=YES+42");
    13. cookieJar->insertCookie(cookie);
    14. manager->setCookieJar(cookieJar);
    15.  
    16. std::cout << termcolor::yellow << "YOUTUBE::makeRequest()" << termcolor::reset<< std::endl;
    17.  
    18. request.setRawHeader("COOKIE" , "CONSENT=YES+42" ); //works
    19. request.setUrl(QUrl("https://" + indexCore ));
    20. manager->get(request);
    21. }
    22.  
    23.  
    24. void Youtube::slotReadyRead(QNetworkReply *replay)
    25. {
    26. QByteArray dataTemp = replay->readAll();
    27. website = dataTemp.toStdString();
    28. manager->cookieJar()->deleteCookie(cookie);
    29. std::cout << termcolor::yellow << "Youtube::slotReadyRead(QNetworkReply *replay)" << termcolor::reset<< std::endl;
    30.  
    31. replay->deleteLater();
    32.  
    33. manager->autoDeleteReplies();
    34. manager->clearAccessCache();
    35. manager->clearConnectionCache();
    36. manager->setAutoDeleteReplies(true);
    37. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: qNetworkAccessManager and cookies

    I do not see how line 28 of the new version can compile, or if it compiles, work. The variable "cookie" is either not defined in this scope, or it is defined but is not the "cookie" of line 13.

    In any case, the point of a cookie is persistence so there seems little to be gained from constantly inserting and delete the same cookie. On your first request a cookie, or cookies, will come back and be placed in the cookie jar. If one of those is this "CONSENT" cookie then you are immediately removing any information the server may have placed in it.

    Put your starting cookie in the jar once in the constructor and not in makeRequest(), then just leave the jar alone as you make requests.

Similar Threads

  1. Replies: 2
    Last Post: 1st March 2012, 10:59
  2. Cookies....Again....
    By TJSonic in forum Qt Programming
    Replies: 7
    Last Post: 22nd November 2011, 09:00
  3. Replies: 0
    Last Post: 31st October 2011, 12:33
  4. QNetworkAccessManager can't handle multiple cookies?
    By krippy2k in forum Qt Programming
    Replies: 5
    Last Post: 7th June 2009, 23:12
  5. I get only 1 of 4 cookies...
    By cherva in forum Qt Programming
    Replies: 33
    Last Post: 5th June 2009, 15:42

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.