Results 1 to 14 of 14

Thread: How do I send data with http post request?

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How do I send data with http post request?

    I want to send a lot of data to a webpage with a post requst. The data is a username and a long message text.
    I look at the QHttp class but I can't seem to find how to send all that data. I also need to read a reply from the webpage (it will reply with a simple string.
    Does anyone have a good short example on this?

  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: How do I send data with http post request?

    QHttp::post() (you can pass it a device to read from and to write to) or better yet use QNetworkAccessManager.

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I send data with http post request?

    I'm sorry, but I'm still confused on the usage of these two. Is there any example of how to send the acctual data and receive the answer?

  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: How do I send data with http post request?

    What is exactly that you don't understand? This is a single function call dependant of the source and destination of the data, so I'm not sure what kind of example would you like to see.

    Qt Code:
    1. QIODevice *outgoing = ...;
    2. QIODevice *incoming = ...;
    3. QHttp *http = ...;
    4. // this one
    5. connect(http, SIGNAL(done(bool)), ...);
    6. // or this one
    7. connect(incoming, SIGNAL(readyRead()), ...);
    8. // or something else...
    9. int id = http->post("/somepath", outgoing, incoming);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I send data with http post request?

    Well, first of all, I want to send data to a php page so that the php page can read values from the POST variables $_POST['foo'] $_POST['bar'] (excuse me for the php code).

  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: How do I send data with http post request?

    And the problem is you don't know how to format the data, right? Sorry but this is out of scope of this forum. I suggest you take a network sniffer such as iptraf, ettercap or tcpdump, send a post request to some php page and see how the data is formatted.

  7. #7
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I send data with http post request?

    Yes that's probably it. Thanks anyway.

  8. #8
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I send data with http post request?

    Ah, I think I've solved it now.

    Qt Code:
    1. QByteArray* ba=new QByteArray("a=aaa&b=bbb&ok=ok");
    2. const QString s("/url.php Accept: text/html, text /plain, text/css, text/sgml, */* ;q=0.01\nAccept-Encoding : gzip, bzip2\nAccept-Language: en\nPragma: no-cache\nCache-Control: no-cache\nUser-Agent: Foobar\nReferer: http://localhost /a.php\nContent-type:application/x-www-form-urlencoded\nContent-lentgh::17\n");
    3. QHttpobject->post( s, *ba);
    To copy to clipboard, switch view to plain text mode 

  9. #9
    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: How do I send data with http post request?

    The headers should be in the data section, not the path section. The path should only contain "/url.php".

  10. #10
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I send data with http post request?

    Well, I couldn't make it work if I put it in the data section. The parameters were not received in the webserver.

  11. #11
    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: How do I send data with http post request?

    Did you remember about the extra newline after the last header?

  12. #12
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I send data with http post request?

    I've added both \n and \n\n after ContentLength: 17 but no success.
    If anyone can re-write it properly, feel free to do so, because I can't make it work

  13. #13
    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: How do I send data with http post request?

    Take a network sniffer or netcat and see what the difference is.

  14. #14
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I send data with http post request?

    Well the difference is big. The real webbrowser gives this extra information

    Qt Code:
    1. User-Agent: Links (2.2; Linux 2.6.25-gentoo-r7 x86_64; 150x41)
    2. Accept: */*
    3. Accept-Encoding: gzip, deflate, bzip2
    4. Accept-Charset: us-ascii, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-13, ISO-8859-14, ISO-8859-15, ISO-8859-16, windows-1250, windows-1251, windows-1252, windows-1256, windows-1257, cp437, cp737, cp850, cp852, cp866, x-cp866-u, x-mac, x-mac-ce, x-kam-cs, koi8-r, koi8-u, koi8-ru, TCVN-5712, VISCII, utf-8
    5. Accept-Language: en, *;q=0.1
    6. Connection: Keep-Alive
    7. Content-Type: application/x-www-form-urlencoded
    To copy to clipboard, switch view to plain text mode 

    And I can not add any of these without adding them to the path.

Similar Threads

  1. Send Base64 encoded data
    By rmagro in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 16:58

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.