PDA

View Full Version : How do I send data with http post request?



Morea
19th January 2009, 08:39
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?

wysota
19th January 2009, 08:46
QHttp::post() (you can pass it a device to read from and to write to) or better yet use QNetworkAccessManager.

Morea
19th January 2009, 13:32
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?

wysota
19th January 2009, 14:10
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.


QIODevice *outgoing = ...;
QIODevice *incoming = ...;
QHttp *http = ...;
// this one
connect(http, SIGNAL(done(bool)), ...);
// or this one
connect(incoming, SIGNAL(readyRead()), ...);
// or something else...
int id = http->post("/somepath", outgoing, incoming);

Morea
19th January 2009, 17:49
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).

wysota
19th January 2009, 17:57
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.

Morea
19th January 2009, 19:08
Yes that's probably it. Thanks anyway.

Morea
20th January 2009, 07:06
Ah, I think I've solved it now.


QByteArray* ba=new QByteArray("a=aaa&b=bbb&ok=ok");
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");
QHttpobject->post( s, *ba);

wysota
20th January 2009, 07:50
The headers should be in the data section, not the path section. The path should only contain "/url.php".

Morea
20th January 2009, 11:54
Well, I couldn't make it work if I put it in the data section. The parameters were not received in the webserver.

wysota
20th January 2009, 13:01
Did you remember about the extra newline after the last header?

Morea
20th January 2009, 18:27
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:confused:

wysota
20th January 2009, 20:01
Take a network sniffer or netcat and see what the difference is.

Morea
21st January 2009, 22:51
Well the difference is big. The real webbrowser gives this extra information


User-Agent: Links (2.2; Linux 2.6.25-gentoo-r7 x86_64; 150x41)
Accept: */*
Accept-Encoding: gzip, deflate, bzip2
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
Accept-Language: en, *;q=0.1
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded


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