|
|
Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QHttp class provides an implementation of the HTTP protocol. More...
#include <qhttp.h>
Inherits QNetworkProtocol.
This class provides two different interfaces: one is the QNetworkProtocol interface that allows you to use HTTP through the QUrlOperator abstraction. The other is 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.
Don't mix the two interfaces, since the behavior is not well-defined.
If you want to use QHttp with the QNetworkProtocol interface, you do not use it directly, but rather through a QUrlOperator, for example:
QUrlOperator op( "http://www.trolltech.com" ); op.get( "index.html" );
This code will only work if the QHttp class is registered; to register the class, you must call qInitNetworkProtocols() before using a QUrlOperator with HTTP.
The QNetworkProtocol interface for HTTP only supports the operations operationGet() and operationPut(), i.e. QUrlOperator::get() and QUrlOperator::put(), if you use it with a QUrlOperator.
The rest of this descrption describes the direct interface to HTTP.
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 progressbar 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 readBlock() 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 reqeusts
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 behaviour, 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 Qt Network Documentation, QNetworkProtocol, QUrlOperator, QFtp, and Input/Output and Networking.
This enum identifies the error that occurred.
See also error().
This enum is used to specify the state the client is in:
See also stateChanged() and state().
See also setHost().
For the current request, the requestFinished() signal with the error argument TRUE is emitted. For all other requests that are affected by the abort(), no signals are emitted.
Since this slot also deletes the scheduled requests, there are no requests left and the done() signal is emitted (with the error argument TRUE).
See also clearPendingRequests().
See also get(), post(), request(), readyRead(), readBlock(), and readAll().
See also hasPendingRequests() and abort().
For the requests issued with get(), post() and head(), QHttp sets the connection to be keep-alive. You can also do this using the header you pass to the request() function. QHttp only closes the connection to the HTTP server if the response header requires it to do so.
The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished().
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
If you want to close the connection immediately, you have to use abort() instead.
See also stateChanged(), abort(), requestStarted(), requestFinished(), and done().
This function can be used to delete the QIODevice in the slot connected to the requestFinished() signal.
See also get(), post(), and request().
See also currentRequest().
See also currentId().
This function can be used to delete the QIODevice in the slot connected to the requestFinished() signal.
See also currentDestinationDevice(), post(), and request().
This signal is emitted when this object reads data from a HTTP server to indicate the current progress of the download.
done is the amount of data that has already arrived and total is the total amount of data. It is possible that the total amount of data that should be transferred cannot be determined, in which case total is 0.(If you connect to a QProgressBar, the progress bar shows a busy indicator if the total is 0).
Warning: done and total are not necessarily the size in bytes, since for large files these values might need to be "scaled" to avoid overflow.
See also dataSendProgress(), get(), post(), request(), and QProgressBar::progress.
This signal is emitted when this object sends data to a HTTP server to inform it about the progress of the upload.
done is the amount of data that has already arrived and total is the total amount of data. It is possible that the total amount of data that should be transferred cannot be determined, in which case total is 0.(If you connect to a QProgressBar, the progress bar shows a busy indicator if the total is 0).
Warning: done and total are not necessarily the size in bytes, since for large files these values might need to be "scaled" to avoid overflow.
See also dataReadProgress(), post(), request(), and QProgressBar::progress.
This signal is emitted when the last pending request has finished; (it is emitted after the last request's requestFinished() signal). error is TRUE if an error occurred during the processing; otherwise error is FALSE.
See also requestFinished(), error(), and errorString().
If you start a new request, the error status is reset to NoError.
path must be an absolute path like /index.html or an absolute URI like http://www.trolltech.com/index.html.
If the IO device to is 0 the readyRead() signal is emitted every time new content data is available to read.
If the IO device to is not 0, the content data of the response is written directly to the device. Make sure that the to pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted).
The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished().
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
See also setHost(), post(), head(), request(), requestStarted(), requestFinished(), and done().
The request that is being executed is not considered as a scheduled request.
See also clearPendingRequests(), currentId(), and currentRequest().
path must be an absolute path like /index.html or an absolute URI like http://www.trolltech.com/index.html.
The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished().
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
See also setHost(), get(), post(), request(), requestStarted(), requestFinished(), and done().
path must be an absolute path like /index.html or an absolute URI like http://www.trolltech.com/index.html.
The incoming data comes via the data IO device.
If the IO device to is 0 the readyRead() signal is emitted every time new content data is available to read.
If the IO device to is not 0, the content data of the response is written directly to the device. Make sure that the to pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted).
The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished().
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
See also setHost(), get(), head(), request(), requestStarted(), requestFinished(), and done().
data is used as the content data of the HTTP request.
See also get(), post(), request(), readyRead(), bytesAvailable(), and readBlock().
See also get(), post(), request(), readyRead(), bytesAvailable(), and readAll().
This signal is emitted when there is new response data to read.
If you specified a device in the request where the data should be written to, then this signal is not emitted; instead the data is written directly to the device.
The response header is passed in resp.
You can read the data with the readAll() or readBlock() functions
This signal is useful if you want to process the data in chunks as soon as it becomes available. If you are only interested in the complete data, just connect to the requestFinished() signal and read the data then instead.
See also get(), post(), request(), readAll(), readBlock(), and bytesAvailable().
The incoming data comes via the data IO device.
If the IO device to is 0 the readyRead() signal is emitted every time new content data is available to read.
If the IO device to is not 0, the content data of the response is written directly to the device. Make sure that the to pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted).
The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished().
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
See also setHost(), get(), post(), head(), requestStarted(), requestFinished(), and done().
data is used as the content data of the HTTP request.
This signal is emitted when processing the request identified by id has finished. error is TRUE if an error occurred during the processing; otherwise error is FALSE.
See also requestStarted(), done(), error(), and errorString().
This signal is emitted when processing the request identified by id starts.
See also requestFinished() and done().
This signal is emitted when the HTTP header of a server response is available. The header is passed in resp.
See also get(), post(), head(), request(), and readyRead().
The function does not block and returns immediately. The request is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by requestStarted() and requestFinished().
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
See also get(), post(), head(), request(), requestStarted(), requestFinished(), and done().
See also State and stateChanged().
This signal is emitted when the state of the QHttp object changes. The argument state is the new state of the connection; it is one of the State values.
This usually happens when a request is started, but it can also happen when the server closes the connection or when a call to closeConnection() succeeded.
See also get(), post(), head(), request(), closeConnection(), state(), and State.
This file is part of the Qt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.
Copyright © 2007 Trolltech | Trademarks | Qt 3.3.8
|