You Are Here:

Community: Wiki

This page was last modified on 28 September 2009, at 08:01.

CS001431 - Creating an HTTP network request in Qt

From Forum Nokia Wiki


ID CS001431 Creation date June 16, 2009
Platform S60 3rd Edition, FP1, FP2
S60 5th Edition
Tested on devices Nokia 5800 XpressMusic
Category Qt for Symbian Subcategory Networking


Keywords (APIs, classes, methods, functions): QNetworkAccessManager, QUrl, QNetworkReply, QNetworkRequest

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 for S60 installed on your platform.


Preconditions


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

Postconditions

An HTTP response is received.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fCS001424E5fE2dE5fUsingE5ftheE5fE44E45PE4cOYME45NTE5fkeywordE5finE5faE5fE51tE5fpackageE5ffileX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZE71tQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZuserE5ftagQSxE71tX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ