Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QNetworkAccessManager Class Reference
[QtNetwork module]

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

 #include <QNetworkAccessManager>

Inherits QObject.

Note: All the functions in this class are reentrant.

This class was introduced in Qt 4.4.

Public Types

  • enum Operation { HeadOperation, GetOperation, PutOperation, PostOperation }

Public Functions

  • QNetworkAccessManager ( QObject * parent = 0 )
  • ~QNetworkAccessManager ()
  • QAbstractNetworkCache * cache () const
  • QNetworkCookieJar * cookieJar () const
  • QNetworkReply * get ( const QNetworkRequest & request )
  • QNetworkReply * head ( const QNetworkRequest & request )
  • QNetworkReply * post ( const QNetworkRequest & request, QIODevice * data )
  • QNetworkReply * post ( const QNetworkRequest & request, const QByteArray & data )
  • QNetworkProxy proxy () const
  • QNetworkProxyFactory * proxyFactory () const
  • QNetworkReply * put ( const QNetworkRequest & request, QIODevice * data )
  • QNetworkReply * put ( const QNetworkRequest & request, const QByteArray & data )
  • void setCache ( QAbstractNetworkCache * cache )
  • void setCookieJar ( QNetworkCookieJar * cookieJar )
  • void setProxy ( const QNetworkProxy & proxy )
  • void setProxyFactory ( QNetworkProxyFactory * factory )
  • 29 public functions inherited from QObject

Signals

Protected Functions

  • virtual QNetworkReply * createRequest ( Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0 )
  • 7 protected functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 5 static public members inherited from QObject

Detailed Description

The QNetworkAccessManager class allows the application to post network requests and receive replies

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.

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, SIGNAL(finished(QNetworkReply*)),
         this, SLOT(replyFinished(QNetworkReply*)));

 manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));

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.

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

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

 QNetworkReply *reply = manager->get(request);
 connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
 connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
         this, SLOT(slotError(QNetworkReply::NetworkError)));
 connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
         this, SLOT(slotSslErrors(QList<QSslError>)));

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())

See also QNetworkReply::operation().


Member Function Documentation

QNetworkAccessManager::QNetworkAccessManager ( QObject * parent = 0 )

Constructs a QNetworkAccessManager object that is the center of the Network Access API and sets parent as the parent object.

QNetworkAccessManager::~QNetworkAccessManager ()

Destroys the QNetworkAccessManager object and frees up any resources. Note that QNetworkReply objects that are returned from this class have this object set as their parents, which means that they will be deleted along with it if you don't call QObject::setParent() on them.

void QNetworkAccessManager::authenticationRequired ( QNetworkReply * reply, QAuthenticator * authenticator )   [signal]

This signal is emitted whenever a final server requests authentication before it delivers the requested contents. The slot connected to this signal should fill the credentials for the contents (which can be determined by inspecting the reply object) in the authenticator object.

QNetworkAccessManager will cache the credentials internally and will send the same values if the server requires authentication again, without emitting the authenticationRequired() signal. If it rejects the credentials, this signal will be emitted again.

See also proxyAuthenticationRequired().

QAbstractNetworkCache * QNetworkAccessManager::cache () const

Returns the cache that is used to store data obtained from the network.

This function was introduced in Qt 4.5.

See also setCache().

QNetworkCookieJar * QNetworkAccessManager::cookieJar () const

Returns the QNetworkCookieJar that is used to store cookies obtained from the network as well as cookies that are about to be sent.

See also setCookieJar().

QNetworkReply * QNetworkAccessManager::createRequest ( Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0 )   [virtual protected]

Returns a new QNetworkReply object to handle the operation op and request req. The device outgoingData is always 0 for Get and Head requests, but is the value passed to post() and put() in those operations (the QByteArray variants will pass a QBuffer object).

The default implementation calls QNetworkCookieJar::cookiesForUrl() on the cookie jar set with setCookieJar() to obtain the cookies to be sent to the remote server.

The returned object must be in an open state.

void QNetworkAccessManager::finished ( QNetworkReply * reply )   [signal]

This signal is emitted whenever a pending network reply is finished. The reply parameter will contain a pointer to the reply that has just finished. This signal is emitted in tandem with the QNetworkReply::finished() signal.

See QNetworkReply::finished() for information on the status that the object will be in.

Note: Do not delete the reply object in the slot connected to this signal. Use deleteLater().

See also QNetworkReply::finished() and QNetworkReply::error().

QNetworkReply * QNetworkAccessManager::get ( const QNetworkRequest & request )

This function is used to post a request to obtain the contents of the target request. It will cause the contents to be downloaded, along with the headers associated with it. It returns a new QNetworkReply object opened for reading which emits its QIODevice::readyRead() signal whenever new data arrives.

See also post() and put().

QNetworkReply * QNetworkAccessManager::head ( const QNetworkRequest & request )

This function is used to post a request to obtain the network headers for request. It takes its name after the HTTP request associated (HEAD). It returns a new QNetworkReply object which will contain such headers.

QNetworkReply * QNetworkAccessManager::post ( const QNetworkRequest & request, QIODevice * data )

This function is used to send an HTTP POST request to the destination specified by request. The contents of the data device will be uploaded to the server.

data must be opened for reading when this function is called and must remain valid until the finished() signal is emitted for this reply.

The returned QNetworkReply object will be open for reading and will contain the reply sent by the server to the POST request.

Note: sending a POST request on protocols other than HTTP and HTTPS is undefined and will probably fail.

See also get() and put().

QNetworkReply * QNetworkAccessManager::post ( const QNetworkRequest & request, const QByteArray & data )

This is an overloaded function.

This function sends the contents of the data byte array to the destination specified by request.

QNetworkProxy QNetworkAccessManager::proxy () const

Returns the QNetworkProxy that the requests sent using this QNetworkAccessManager object will use. The default value for the proxy is QNetworkProxy::DefaultProxy.

See also setProxy(), setProxyFactory(), and proxyAuthenticationRequired().

void QNetworkAccessManager::proxyAuthenticationRequired ( const QNetworkProxy & proxy, QAuthenticator * authenticator )   [signal]

This signal is emitted whenever a proxy requests authentication and QNetworkAccessManager cannot find a valid, cached credential. The slot connected to this signal should fill in the credentials for the proxy proxy in the authenticator object.

QNetworkAccessManager will cache the credentials internally. The next time the proxy requests authentication, QNetworkAccessManager will automatically send the same credential without emitting the proxyAuthenticationRequired signal again.

If the proxy rejects the credentials, QNetworkAccessManager will emit the signal again.

See also proxy(), setProxy(), and authenticationRequired().

QNetworkProxyFactory * QNetworkAccessManager::proxyFactory () const

Returns the proxy factory that this QNetworkAccessManager object is using to determine the proxies to be used for requests.

Note that the pointer returned by this function is managed by QNetworkAccessManager and could be deleted at any time.

This function was introduced in Qt 4.5.

See also setProxyFactory() and proxy().

QNetworkReply * QNetworkAccessManager::put ( const QNetworkRequest & request, QIODevice * data )

This function is used to upload the contents of data to the destination request.

data must be opened for reading when this function is called and must remain valid until the finished() signal is emitted for this reply.

The returned QNetworkReply object will be open for reply, but whether anything will be available for reading is protocol dependent. For HTTP, the server may send a small HTML page indicating the upload was successful (or not). Other protocols will probably have content in their replies.

For HTTP, this request will send a PUT request, which most servers do not allow. Form upload mechanisms, including that of uploading files through HTML forms, use the POST mechanism.

See also get() and post().

QNetworkReply * QNetworkAccessManager::put ( const QNetworkRequest & request, const QByteArray & data )

This is an overloaded function.

This function sends the contents of the data byte array to the destination specified by request.

void QNetworkAccessManager::setCache ( QAbstractNetworkCache * cache )

Sets the manager's network cache to be the cache specified. The cache is used for all requests dispatched by the manager.

Use this function to set the network cache object to a class that implements additional features, like saving the cookies to permanent storage.

Note: QNetworkAccessManager takes ownership of the cache object.

QNetworkAccessManager by default does not have a set cache. Qt provides a simple disk cache, QNetworkDiskCache, which can be used.

This function was introduced in Qt 4.5.

See also cache() and QNetworkRequest::CacheLoadControl.

void QNetworkAccessManager::setCookieJar ( QNetworkCookieJar * cookieJar )

Sets the manager's cookie jar to be the cookieJar specified. The cookie jar is used by all requests dispatched by the manager.

Use this function to set the cookie jar object to a class that implements additional features, like saving the cookies to permanent storage.

Note: QNetworkAccessManager takes ownership of the cookieJar object.

QNetworkAccessManager will set the parent of the cookieJar passed to itself, so that the cookie jar is deleted when this object is deleted as well. If you want to share cookie jars between different QNetworkAccessManager objects, you may want to set the cookie jar's parent to 0 after calling this function.

QNetworkAccessManager by default does not implement any cookie policy of its own: it accepts all cookies sent by the server, as long as they are well formed and meet the minimum security requirements (cookie domain matches the request's and cookie path matches the request's). In order to implement your own security policy, override the QNetworkCookieJar::cookiesForUrl() and QNetworkCookieJar::setCookiesFromUrl() virtual functions. Those functions are called by QNetworkAccessManager when it detects a new cookie.

See also cookieJar(), QNetworkCookieJar::cookiesForUrl(), and QNetworkCookieJar::setCookiesFromUrl().

void QNetworkAccessManager::setProxy ( const QNetworkProxy & proxy )

Sets the proxy to be used in future requests to be proxy. This does not affect requests that have already been sent. The proxyAuthenticationRequired() signal will be emitted if the proxy requests authentication.

A proxy set with this function will be used for all requests issued by QNetworkAccessManager. In some cases, it might be necessary to select different proxies depending on the type of request being sent or the destination host. If that's the case, you should consider using setProxyFactory().

See also proxy() and proxyAuthenticationRequired().

void QNetworkAccessManager::setProxyFactory ( QNetworkProxyFactory * factory )

Sets the proxy factory for this class to be factory. A proxy factory is used to determine a more specific list of proxies to be used for a given request, instead of trying to use the same proxy value for all requests.

All queries sent by QNetworkAccessManager will have type QNetworkProxyQuery::UrlRequest.

For example, a proxy factory could apply the following rules:

  • if the target address is in the local network (for example, if the hostname contains no dots or if it's an IP address in the organization's range), return QNetworkProxy::NoProxy
  • if the request is FTP, return an FTP proxy
  • if the request is HTTP or HTTPS, then return an HTTP proxy
  • otherwise, return a SOCKSv5 proxy server

The lifetime of the object factory will be managed by QNetworkAccessManager. It will delete the object when necessary.

Note: If a specific proxy is set with setProxy(), the factory will not be used.

This function was introduced in Qt 4.5.

See also proxyFactory(), setProxy(), and QNetworkProxyQuery.

void QNetworkAccessManager::sslErrors ( QNetworkReply * reply, const QList<QSslError> & errors )   [signal]

This signal is emitted if the SSL/TLS session encountered errors during the set up, including certificate verification errors. The errors parameter contains the list of errors and reply is the QNetworkReply that is encountering these errors.

To indicate that the errors are not fatal and that the connection should proceed, the QNetworkReply::ignoreSslErrors() function should be called from the slot connected to this signal. If it is not called, the SSL session will be torn down before any data is exchanged (including the URL).

This signal can be used to display an error message to the user indicating that security may be compromised and display the SSL settings (see sslConfiguration() to obtain it). If the user decides to proceed after analyzing the remote certificate, the slot should call ignoreSslErrors().

See also QSslSocket::sslErrors(), QNetworkReply::sslErrors(), QNetworkReply::sslConfiguration(), and QNetworkReply::ignoreSslErrors().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année

Le Qt Labs au hasard

Logo

Le moteur de rendu Raster

Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.5
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web