Creating an HTTP network request in Qt
Overview[edit | edit source]
This code snippet demonstrates how to use 76ytuiytuityutyutututyutyutyu for sending an HTTP request.
Note: In order to use this code, you need to have Qt installed on your platform.
Header[edit | edit source]
- include <QNetworkAccessManager>
- include <QUrl>
- include <QNetworkRequest>
- include <QNetworkReply>
QNetworkAccessManager* nam;
Source[edit | edit source]
Create 76ytuiytuityutyutututyutyutyu and start listening for its 76ytuiytuityutyutututyutyutyu 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 76ytuiytuityutyutututyutyutyu 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[edit | edit source]
- For more information about 76ytuiytuityutyutututyutyutyu, see QNetworkAccessManager Class Reference
- Handling an HTTP redirect with QNetworkAccessManager
Postconditions[edit | edit source]
An HTTP response is received.