Logo Search packages:      
Sourcecode: qt4-x11 version File versions

QHttp Class Reference

Inheritance diagram for QHttp:


List of all members.

Detailed Description

The QHttp class provides an implementation of the HTTP protocol.


This class provides a direct interface to HTTP that allows you to have more control over the requests and that allows you to access the response header fields.

The class works asynchronously, so there are no blocking functions. If an operation cannot be executed immediately, the function will still return straight away and the operation will be scheduled for later execution. The results of scheduled operations are reported via signals. This approach depends on the event loop being in operation.

The operations that can be scheduled (they are called "requests" in the rest of the documentation) are the following: setHost(), get(), post(), head() and request().

All of these requests return a unique identifier that allows you to keep track of the request that is currently executed. When the execution of a request starts, the requestStarted() signal with the identifier is emitted and when the request is finished, the requestFinished() signal is emitted with the identifier and a bool that indicates if the request finished with an error.

To make an HTTP request you must set up suitable HTTP headers. The following example demonstrates, how to request the main HTML page from the Trolltech home page (i.e. the URL http://www.trolltech.com/index.html):

doc/src/snippets/code/src.network.access.qhttp.cpp 2

For the common HTTP requests GET, POST and HEAD, QHttp provides the convenience functions get(), post() and head(). They already use a reasonable header and if you don't have to set special header fields, they are easier to use. The above example can also be written as:

doc/src/snippets/code/src.network.access.qhttp.cpp 3

For this example the following sequence of signals is emitted (with small variations, depending on network traffic, etc.):

doc/src/snippets/code/src.network.access.qhttp.cpp 4

The dataSendProgress() and dataReadProgress() signals in the above example are useful if you want to show a progress bar to inform the user about the progress of the download. The second argument is the total size of data. In certain cases it is not possible to know the total amount in advance, in which case the second argument is 0. (If you connect to a QProgressBar a total of 0 results in a busy indicator.)

When the response header is read, it is reported with the responseHeaderReceived() signal.

The readyRead() signal tells you that there is data ready to be read. The amount of data can then be queried with the bytesAvailable() function and it can be read with the read() or readAll() functions.

If an error occurs during the execution of one of the commands in a sequence of commands, all the pending commands (i.e. scheduled, but not yet executed commands) are cleared and no signals are emitted for them.

For example, if you have the following sequence of requests

doc/src/snippets/code/src.network.access.qhttp.cpp 5

and the get() request fails because the host lookup fails, then the post() request is never executed and the signals would look like this:

doc/src/snippets/code/src.network.access.qhttp.cpp 6

You can then get details about the error with the error() and errorString() functions. Note that only unexpected behavior, like network failure is considered as an error. If the server response contains an error status, like a 404 response, this is reported as a normal response case. So you should always check the status code of the response header.

The functions currentId() and currentRequest() provide more information about the currently executing request.

The functions hasPendingRequests() and clearPendingRequests() allow you to query and clear the list of pending requests.

See also:
QFtp, {HTTP Example}, {Torrent Example}

Definition at line 174 of file qhttp.h.

Public Types

enum  ConnectionMode { ConnectionModeHttp, ConnectionModeHttps }
enum  Error {
  NoError, UnknownError, HostNotFound, ConnectionRefused,
  UnexpectedClose, InvalidResponseHeader, WrongContentLength, Aborted,
  AuthenticationRequiredError, ProxyAuthenticationRequiredError
enum  State {
  Unconnected, HostLookup, Connecting, Sending,
  Reading, Connected, Closing

Public Slots

void abort ()
void deleteLater ()
void ignoreSslErrors ()


void authenticationRequired (const QString &hostname, quint16 port, QAuthenticator *)
void dataReadProgress (int, int)
void dataSendProgress (int, int)
void destroyed (QObject *=0)
void done (bool)
void proxyAuthenticationRequired (const QNetworkProxy &proxy, QAuthenticator *)
void readyRead (const QHttpResponseHeader &resp)
void requestFinished (int, bool)
void requestStarted (int)
void responseHeaderReceived (const QHttpResponseHeader &resp)
void sslErrors (const QList< QSslError > &errors)
void stateChanged (int)

Public Member Functions

bool blockSignals (bool b)
qint64 bytesAvailable () const
const QObjectListchildren () const
void clearPendingRequests ()
int close ()
int closeConnection ()
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
QIODevicecurrentDestinationDevice () const
int currentId () const
QHttpRequestHeader currentRequest () const
QIODevicecurrentSourceDevice () const
bool disconnect (const QObject *receiver, const char *member=0)
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
void dumpObjectInfo ()
void dumpObjectTree ()
QList< QByteArraydynamicPropertyNames () const
Error error () const
QString errorString () const
virtual bool event (QEvent *)
virtual bool eventFilter (QObject *, QEvent *)
template<typename T>
findChild (const QString &aName=QString()) const
template<typename T>
QList< T > findChildren (const QRegExp &re) const
template<typename T>
QList< T > findChildren (const QString &aName=QString()) const
int get (const QString &path, QIODevice *to=0)
bool hasPendingRequests () const
int head (const QString &path)
bool inherits (const char *classname) const
void installEventFilter (QObject *)
bool isWidgetType () const
void killTimer (int id)
QHttpResponseHeader lastResponse () const
void moveToThread (QThread *thread)
QString objectName () const
QObjectparent () const
int post (const QString &path, const QByteArray &data, QIODevice *to=0)
int post (const QString &path, QIODevice *data, QIODevice *to=0)
QVariant property (const char *name) const
 QHttp (const QString &hostname, ConnectionMode mode, quint16 port=0, QObject *parent=0)
 QHttp (const QString &hostname, quint16 port=80, QObject *parent=0)
 QHttp (QObject *parent=0)
qint64 read (char *data, qint64 maxlen)
QByteArray readAll ()
void removeEventFilter (QObject *)
int request (const QHttpRequestHeader &header, const QByteArray &data, QIODevice *to=0)
int request (const QHttpRequestHeader &header, QIODevice *device=0, QIODevice *to=0)
int setHost (const QString &hostname, ConnectionMode mode, quint16 port=0)
int setHost (const QString &hostname, quint16 port=80)
void setObjectName (const QString &name)
void setParent (QObject *)
bool setProperty (const char *name, const QVariant &value)
int setProxy (const QNetworkProxy &proxy)
int setProxy (const QString &host, int port, const QString &username=QString(), const QString &password=QString())
int setSocket (QTcpSocket *socket)
int setUser (const QString &username, const QString &password=QString())
void setUserData (uint id, QObjectUserData *data)
bool signalsBlocked () const
int startTimer (int interval)
State state () const
QThreadthread () const
QObjectUserData * userData (uint id) const
virtual ~QHttp ()

Static Public Member Functions

static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
static uint registerUserData ()

Protected Member Functions

virtual void childEvent (QChildEvent *)
virtual void connectNotify (const char *signal)
virtual void customEvent (QEvent *)
virtual void disconnectNotify (const char *signal)
int receivers (const char *signal) const
QObjectsender () const
virtual void timerEvent (QTimerEvent *)

Protected Attributes

QObjectData * d_ptr

Static Protected Attributes

static const QMetaObject staticQtMetaObject


QString objectName
 the name of this object

Private Member Functions

void void void void void _q_slotBytesWritten (qint64 numBytes)) Q_PRIVATE_SLOT(d_func()
void void void void _q_slotClosed ()) Q_PRIVATE_SLOT(d_func()
void void _q_slotConnected ()) Q_PRIVATE_SLOT(d_func()
void void void void void void _q_slotDoFinished ()) Q_PRIVATE_SLOT(d_func()
void void void _q_slotError (QAbstractSocket::SocketError)) Q_PRIVATE_SLOT(d_func()
void _q_slotReadyRead ()) Q_PRIVATE_SLOT(d_func()
void void void void void void void _q_slotSendRequest ()) friend class QHttpNormalRequest
 Q_PRIVATE_SLOT (d_func(), void _q_startNextRequest()) Q_PRIVATE_SLOT(d_func()


class QApplication
class QApplicationPrivate
class QCoreApplication
class QCoreApplicationPrivate
class QHttpCloseRequest
class QHttpPGHRequest
class QHttpSetHostRequest
class QHttpSetProxyRequest
class QHttpSetSocketRequest
class QHttpSetUserRequest
struct QMetaObject
class QThreadData
class QWidget

Related Functions

(Note that these are not member functions.)

qFindChild (const QObject *obj, const QString &name=QString(), T dummy=0)
qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QString &name=QString(), T dummy=0)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
T * qobject_cast (QObject *object)
void * qt_find_obj_child (QObject *parent, const char *type, const QString &name)

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index