Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 10:02, 2 October 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Creating an HTTP network request in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Keywords: QNetworkAccessManager, QUrl, QNetworkReply, QNetworkRequest
Created: tepaa (04 Jun 2009)
Last edited: hamishwillee (02 Oct 2013)



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.


#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
QNetworkAccessManager* nam;


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("");
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 =
// Or the target URL if it was a redirect:
QVariant redirectionTargetUrl =
// 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 =;
// Example 2: Reading bytes form the reply
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
// Some http error received
// handle errors here
// We receive ownership of the reply object
// and therefore need to handle deletion.
delete reply;

See also


An HTTP response is received.

This page was last modified on 2 October 2013, at 10:02.
935 page views in the last 30 days.