[Qt-interest] How to: send http POST data to server.

Bryce Allen qt at bda.ath.cx
Mon Feb 15 16:07:56 CET 2010


> Message: 3
> Date: Mon, 15 Feb 2010 14:14:21 +0530
> From: Mohammed Imran B <imran.ar20487 at gmail.com>
> Subject: [Qt-interest] How to: send http POST data to server.
> To: qt-interest at trolltech.com
> Message-ID:
> 	<88b98fe21002150044r6063a61bi11c82ebe3f5b69d1 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hi,
> 
> I have the data in my vector and I want to send the data containing
> in that vector to the servlet residing on a different machine.
> 
> can any body tell me how do do it. Is there any sample example of
> having a similar functionalities.
> 
> Thanks and regards,
> 
> Imran.

I use QUrl to URL encode the data, it was not obvious how to do that
from the examples. You could iterate over the vector and add each value
with addQueryItem. Here's an example snipet:

QNetworkAccessManager *manager = new QNetworkAccessManager;
connect(manager, SIGNAL(finished(QNetworkReply*)),
        this, SLOT(replyFinished(QNetworkReply*)));
// Define replyFinished slot to handle the reply.

...

QNetworkRequest request;
QUrl tmpUrl;
request.setUrl(QUrl("http://localhost/test.php"));
request.setHeader(QNetworkRequest::ContentTypeHeader,
                  "application/x-www-form-urlencoded");

tmpUrl.addQueryItem("field1", "value1");
tmpUrl.addQueryItem("field2", "value2");
...

QNetworkReply *reply = manager->post(request, tmpUrl.encodedQuery());



More information about the Qt-interest-old mailing list