QNetworkAccessManager Class

The QNetworkAccessManager class allows the application to send network requests and receive replies. More...

Header: #include <QNetworkAccessManager>
qmake: QT += network
Since: Qt 4.4
Inherits: QObject

This class was introduced in Qt 4.4.

Note: All functions in this class are reentrant.

Public Types

enum Operation { HeadOperation, GetOperation, PutOperation, PostOperation, DeleteOperation, CustomOperation }

Detailed Description

The Network Access API is constructed around one QNetworkAccessManager object, which holds the common configuration and settings for the requests it sends. It contains the proxy and cache configuration, as well as the signals related to such issues, and reply signals that can be used to monitor the progress of a network operation. One QNetworkAccessManager instance should be enough for the whole Qt application. Since QNetworkAccessManager is based on QObject, it can only be used from the thread it belongs to.

Once a QNetworkAccessManager object has been created, the application can use it to send requests over the network. A group of standard functions are supplied that take a request and optional data, and each return a QNetworkReply object. The returned object is used to obtain any data returned in response to the corresponding request.

A simple download off the network could be accomplished with:

 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
 connect(manager, &QNetworkAccessManager::finished,
         this, &MyClass::replyFinished);

 manager->get(QNetworkRequest(QUrl("http://qt-project.org")));

QNetworkAccessManager has an asynchronous API. When the replyFinished slot above is called, the parameter it takes is the QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.).

Note: After the request has finished, it is the responsibility of the user to delete the QNetworkReply object at an appropriate time. Do not directly delete it inside the slot connected to finished(). You can use the deleteLater() function.

Note: QNetworkAccessManager queues the requests it receives. The number of requests executed in parallel is dependent on the protocol. Currently, for the HTTP protocol on desktop platforms, 6 requests are executed in parallel for one host/port combination.

A more involved example, assuming the manager is already existent, can be:

 QNetworkRequest request;
 request.setUrl(QUrl("http://qt-project.org"));
 request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");

 QNetworkReply *reply = manager->get(request);
 connect(reply, &QIODevice::readyRead, this, &MyClass::slotReadyRead);
 connect(reply, &QNetworkReply::errorOccurred,
         this, &MyClass::slotError);
 connect(reply, &QNetworkReply::sslErrors,
         this, &MyClass::slotSslErrors);

See also QNetworkRequest, QNetworkReply, and QNetworkProxy.

Member Type Documentation

enum QNetworkAccessManager::Operation

Indicates the operation this reply is processing.

ConstantValueDescription
QNetworkAccessManager::HeadOperation1retrieve headers operation (created with head())
QNetworkAccessManager::GetOperation2retrieve headers and download contents (created with get())
QNetworkAccessManager::PutOperation3upload contents operation (created with put())
QNetworkAccessManager::PostOperation4send the contents of an HTML form for processing via HTTP POST (created with post())
QNetworkAccessManager::DeleteOperation5delete contents operation (created with deleteResource())
QNetworkAccessManager::CustomOperation6custom operation (created with sendCustomRequest())

This enum was introduced or modified in Qt 4.7.

See also QNetworkReply::operation().