PDA

View Full Version : Send Qt HTTP Post and read back the response



Pufo
26th April 2011, 19:52
I'm using this code to make a simple HTTP Post ( a login )



QNetworkAccessManager *nwam = new QNetworkAccessManager;

QNetworkRequest request(QUrl("http://localhost/laptop/trylogin.php"));

QByteArray data;
QUrl params;

QString userString(user);
QString passString(pass);

params.addQueryItem("user", userString );
params.addQueryItem("pass", passString );
data.append(params.toString());
data.remove(0,1);

QNetworkReply *reply = nwam->post(request,data);

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 ?

ChrisW67
26th April 2011, 22:35
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)

Pufo
27th April 2011, 14:28
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.

Pufo
28th April 2011, 20:22
No one ?
Or i wasn't clear in my request ? :(

wysota
28th April 2011, 20:27
You want us to help you with your php programming? The side of Qt has already been explained by ChrisW67.

Pufo
28th April 2011, 21:37
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.

ChrisW67
28th April 2011, 22:59
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 (http://en.wikipedia.org/wiki/IP_over_Avian_Carriers)

This is not the place for it but, to send a response back from PHP you usually use:

header() (http://php.net/manual/en/function.header.php) 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.

Pufo
28th April 2011, 23:04
Thanks for your reply.

I've just edited my previous post :)
Thank you anyway :)

kingk110
10th October 2012, 15:41
how can i use the same code but for get and not to post to the http ??

ChrisW67
10th October 2012, 22:55
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