Results 1 to 4 of 4

Thread: Problem handling cookies with raw HTTP post request

  1. #1
    Join Date
    Mar 2013
    Location
    Slovakia
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Problem handling cookies with raw HTTP post request

    Hello, I'm new to Qt and I hope that someone can help me.

    What I want to do is to get some data from index.php as a logged user and parse it later in my app. So I created a post request on index.php with regular post data for a form (username, password, action - all this stuff was sniffed when I was logging in with browser - so its exactly what the php expects), but I fail to handle cookies. Let me show the code:

    Qt Code:
    1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    2.  
    3. connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
    4.  
    5. QNetworkRequest request;
    6. request.setUrl(QUrl("https://serveradress/index.php"));
    7.  
    8. QByteArray postData;
    9. postData.append("Cookie: PHPSESSID=randomnumber;\n\n"); // sniffer I use says that no cookies were sent, but if I leave this out server says that I should enable cookies
    10. postData.append("username=mylogin&password=mypassword&action=login"); // post data
    11.  
    12.  
    13. manager->post(QNetworkRequest(request),postData);
    To copy to clipboard, switch view to plain text mode 

    But it still won't log me in (it says that username is empty - I noticed that after I log in with browser, server sends back more cookies containing also one username).
    So I would like to handle cookies otherwise, from what I understand I need to get a fresh cookie from server and use that for further requests - or can I handle this in one single request, ignoring the cookies that server sends back to me ?

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

    Default Re: Problem handling cookies with raw HTTP post request

    Cookies are not part of the message body but rather part of headers. Use QNetworkRequest::setHeader() or QNetworkRequest::setRawHeader() to set those.
    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. The following user says thank you to wysota for this useful post:

    ntXn (18th March 2013)

  4. #3
    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: Problem handling cookies with raw HTTP post request

    If you do a get plain GET on the login page to obtain session cookies (usually all that is required) you can let QNetworkAccessManager worry about sending the cookies with every subsequent request (including your POST). Set a custom QNetworkCookieJar on the QNetworkAccessManager if you need finer control.

  5. #4
    Join Date
    Oct 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem handling cookies with raw HTTP post request

    I have exactly same problem. Could you please respond how you solved this or someone else please explain more in depth.

    I tryed to use this line but it doesn't fix anything.

    Qt Code:
    1. req.setRawHeader("Cookie: PHPSESSID", "19465198465198451681681");
    To copy to clipboard, switch view to plain text mode 



    and this is my thread please someone check it.

Similar Threads

  1. QT Http Post Request Problem
    By zzzuui in forum General Programming
    Replies: 0
    Last Post: 30th December 2012, 13:16
  2. Simplest way to make an HTTP POST/GET request?
    By N3wb in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2012, 04:54
  3. How can I create HTTP post request with qt
    By vinayaka in forum Newbie
    Replies: 1
    Last Post: 10th December 2011, 09:00
  4. HTTP request POST in QT application
    By abdul_moiz in forum Newbie
    Replies: 5
    Last Post: 24th June 2011, 03:48
  5. QT HTTP Post issue when server requires cookies
    By shadyabhi in forum Qt Programming
    Replies: 0
    Last Post: 22nd December 2010, 13:10

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
  •  
Qt is a trademark of The Qt Company.