Results 1 to 10 of 10

Thread: Send Qt HTTP Post and read back the response

  1. #1
    Join Date
    Apr 2011
    Posts
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Send Qt HTTP Post and read back the response

    I'm using this code to make a simple HTTP Post ( a login )

    Qt Code:
    1. QNetworkAccessManager *nwam = new QNetworkAccessManager;
    2.  
    3. QNetworkRequest request(QUrl("http://localhost/laptop/trylogin.php"));
    4.  
    5. QByteArray data;
    6. QUrl params;
    7.  
    8. QString userString(user);
    9. QString passString(pass);
    10.  
    11. params.addQueryItem("user", userString );
    12. params.addQueryItem("pass", passString );
    13. data.append(params.toString());
    14. data.remove(0,1);
    15.  
    16. QNetworkReply *reply = nwam->post(request,data);
    To copy to clipboard, switch view to plain text mode 

    If the logging succeedes or not, how do i send and read the response in Qt ?
    Or, which is the best way to do this ?

  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: Send Qt HTTP Post and read back the response

    You have already sent the request. You should use the QUrl::encodedQuery() as the POST payload. The result of toString() will not be encoded.

    To receive any response you need to connect slots you write to the signals of the QNetworkReply or the QNetworkAccessManager. It is all in the docs and numerous network examples (examples-network)

  3. #3
    Join Date
    Apr 2011
    Posts
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Send Qt HTTP Post and read back the response

    I don't understand exactly how to use encodedQuery, and i think my solution was bad to begin with.
    Also, i can't find an example that does what i need.

    What i need:
    - i have a PHP page which gets 2 post variables, and uses them to try to authenticate
    - i need to send those 2 variables using Qt ( which i did ), and i need to get a response back

    What i don't know how to do:
    - i don't know how to send the response back from PHP
    - i don't know how to read it from Qt

    I've tried to look in QNetworkReply::error(), but i'm getting NoError even it the file i'm posting to is not there.

  4. #4
    Join Date
    Apr 2011
    Posts
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Send Qt HTTP Post and read back the response

    No one ?
    Or i wasn't clear in my request ?

  5. #5
    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: Send Qt HTTP Post and read back the response

    You want us to help you with your php programming? The side of Qt has already been explained by ChrisW67.
    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.


  6. #6
    Join Date
    Apr 2011
    Posts
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Send Qt HTTP Post and read back the response

    Finally !

    I've implemented a SLOT for the finished SIGNAL of the QNetworkAccessManager instance, and put the readAll() output in a QByteArray.

    If i just echo something on the website, i can read it from my QByteArray.
    Last edited by Pufo; 29th April 2011 at 00:03.

  7. #7
    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: Send Qt HTTP Post and read back the response

    For more on the Qt side:
    • Open Qt Assistant
    • Look for "Network examples" in the Index
    • Read the "HTTP Example" particularly HttpWindow::startRequest() and the signal/slot connections it makes.

    There is nothing different about a response from a PHP script versus a response from a Python script, VBScript, or custom web server via wire or other transport

    This is not the place for it but, to send a response back from PHP you usually use:
    • header() to return an HTTP response code like "HTTP/1.0 403 Forbidden"
    • header() to return a redirect to another page (usually on success)
    Anything else you print, echo or otherwise output comes back as the reply body and is available to the Qt program through the QNetworkReply.
    Last edited by ChrisW67; 29th April 2011 at 00:10.

  8. #8
    Join Date
    Apr 2011
    Posts
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Send Qt HTTP Post and read back the response

    Thanks for your reply.

    I've just edited my previous post
    Thank you anyway

  9. #9
    Join Date
    Oct 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Send Qt HTTP Post and read back the response

    how can i use the same code but for get and not to post to the http ??

  10. #10
    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: Send Qt HTTP Post and read back the response

    You can't use the same code for GET because the code POSTs.

    Build a suitable QUrl with your query parameters and use QNetworkAccessManager::get(). It's all in the examples, particularly the already mentioned HTTP Example
    Last edited by wysota; 11th October 2012 at 00:02. Reason: Corrected tags

Similar Threads

  1. How to parse xml from a http xml response?
    By dineshkumar in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2011, 08:28
  2. HTTP POST method
    By Macok in forum Qt Programming
    Replies: 5
    Last Post: 25th July 2010, 15:33
  3. Http post
    By Max123 in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2010, 14:43
  4. QHTTP does not see tunneled TCP HTTP OK authentication response
    By SailingDreams in forum Qt Programming
    Replies: 6
    Last Post: 23rd May 2009, 10:39
  5. How do I send data with http post request?
    By Morea in forum Qt Programming
    Replies: 13
    Last Post: 21st January 2009, 23:51

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.