Creating an HTTP network request in Qt
Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
S60 5th Edition
Article
Keywords: QNetworkAccessManager, QUrl, QNetworkReply, QNetworkRequest
Created: tepaa
(04 Jun 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This code snippet demonstrates how to use QNetworkAccessManager for sending an HTTP request.
Note: In order to use this code, you need to have Qt installed on your platform.
Header
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
QNetworkAccessManager* nam;
Source
Create QNetworkAccessManager and start listening for its finished signal.
nam = new QNetworkAccessManager(this);
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
HTTP GET request:
QUrl url("http://www.forum.nokia.wiki");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
// NOTE: Store QNetworkReply pointer (maybe into caller).
// When this HTTP request is finished you will receive this same
// QNetworkReply as response parameter.
// By the QNetworkReply pointer you can identify request and response.
When the QNetworkAccessManager::finished signal is received, the HTTP request is completed.
void MyHttpEngine::finishedSlot(QNetworkReply* reply)
{
// Reading attributes of the reply
// e.g. the HTTP status code
QVariant statusCodeV =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
// Or the target URL if it was a redirect:
QVariant redirectionTargetUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
// see CS001432 on how to handle this
// no error received?
if (reply->error() == QNetworkReply::NoError)
{
// read data from QNetworkReply here
// Example 1: Creating QImage from the reply
QImageReader imageReader(reply);
QImage pic = imageReader.read();
// Example 2: Reading bytes form the reply
QByteArray bytes = reply.readAll(); // bytes
QString string(bytes); // string
}
// Some http error received
else
{
// handle errors here
}
// We receive ownership of the reply object
// and therefore need to handle deletion.
delete reply;
}
See also
- For more information about QNetworkAccessManager, see QNetworkAccessManager Class Reference
- Handling an HTTP redirect with QNetworkAccessManager
Postconditions
An HTTP response is received.


Creating an HTTP network request is one of the baseline tasks required of most developers in this day and age. This article spells out in clear, yet concise steps, the information required to accomplish this task.
In addition to a relevant list of preconditions, this article steps the reader through the relevant coding sections in a well thought out manner. The frequent line level coding documentation was useful for this reader (and better than his own). In the example code, the author demonstrates two alternate ways of using the reply read from the network, both of which will be helpful to developers that are new to QT.
--beakesland 21:26, 24 September 2009 (UTC)
This code goes against QT's own documentation
From the Article:
QT documentation (for signal finished()):
Suggest changing the code to reply.deleteLater();
pasihir 12:05, 16 November 2010 (UTC)
This code:
is wrong. Use
where XXX is the encoding of the data in the bytearray, ie fromUtf8(...)
It would be really helpful to have Qt Code double-checked by someone from Qt Dev, or at the very least allow editing of this kind of articles. Wrong information is worse than no information.
-apoenitz2 21:24, 19 May 2011
Hi apoenitz2
When you add a response to a comment, please add a signature (and ideally a line afterwards using "----"). There is a signature button in the toolbar.
In the near future we are going to display comments below the main wiki text (see here). This means your corrections and clarifications will be visible below the main article. This should at least make them more obvious.
I can't immediately get the permission locks removed from knowledgebase articles - the idea is that the knowledge base articles are properly reviewed and are locked to prevent inappropriate editing. I will also see if I can get this policy reconsidered but that will take some time.
--hamishwillee 02:17, 20 May 2011 (EEST)