Home · Overviews · Examples 

QHttp Class Reference
[com.trolltech.qt.network module]

The QHttp class provides an implementation of the HTTP protocol. More...

Inherits QObject.


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

    QHttpRequestHeader header("GET", "/index.html");
    header.setValue("Host", "www.trolltech.com");
    http->setHost("www.trolltech.com");
    http->request(header);

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:

    http->setHost("www.trolltech.com"); // id == 1
    http->get("/index.html");           // id == 2

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

    requestStarted(1)
    requestFinished(1, false)

    requestStarted(2)
    stateChanged(Connecting)
    stateChanged(Sending)
    dataSendProgress(77, 77)
    stateChanged(Reading)
    responseHeaderReceived(responseheader)
    dataReadProgress(5388, 0)
    readyRead(responseheader)
    dataReadProgress(18300, 0)
    readyRead(responseheader)
    stateChanged(Connected)
    requestFinished(2, false)

    done(false)

    stateChanged(Closing)
    stateChanged(Unconnected)

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

    http->setHost("www.foo.bar");       // id == 1
    http->get("/index.html");           // id == 2
    http->post("register.html", data);  // id == 3

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

    requestStarted(1)
    requestFinished(1, false)

    requestStarted(2)
    stateChanged(HostLookup)
    requestFinished(2, true)

    done(true)

    stateChanged(Unconnected)

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, and Torrent Example.


Copyright © 2007 Trolltech Trademarks
Qt Jambi 4.3.2_01