This class provides two different interfaces: one is the TQNetworkProtocol interface that allows you to use HTTP through the TQUrlOperator 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.
This code will only work if the TQHttp class is registered; to register the class, you must call tqInitNetworkProtocols() before using a TQUrlOperator with HTTP.
The TQNetworkProtocol interface for HTTP only supports the operations operationGet() and operationPut(), i.e. TQUrlOperator::get() and TQUrlOperator::put(), if you use it with a TQUrlOperator.
The rest of this descrption describes the direct interface to HTTP.
.PP
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.
.PP
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().
.PP
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.
.PP
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):
For the common HTTP requests \fCGET\fR, \fCPOST\fR and \fCHEAD\fR, TQHttp 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:
For this example the following sequence of signals is emitted (with small variations, depending on network traffic, etc.):
.PP
.nf
.br
requestStarted( 1 )
.br
requestFinished( 1, FALSE )
.br
.br
requestStarted( 2 )
.br
stateChanged( Connecting )
.br
stateChanged( Sending )
.br
dataSendProgress( 77, 77 )
.br
stateChanged( Reading )
.br
responseHeaderReceived( responseheader )
.br
dataReadProgress( 5388, 0 )
.br
readyRead( responseheader )
.br
dataReadProgress( 18300, 0 )
.br
readyRead( responseheader )
.br
stateChanged( Connected )
.br
requestFinished( 2, FALSE )
.br
.br
done( FALSE )
.br
.br
stateChanged( Closing )
.br
stateChanged( Unconnected )
.br
.fi
.PP
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.)
.PP
When the response header is read, it is reported with the responseHeaderReceived() signal.
.PP
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.
.PP
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.
.PP
For example, if you have the following sequence of reqeusts
.PP
.nf
.br
http->setHost( "www.foo.bar" ); // id == 1
.br
http->get( "/index.html" ); // id == 2
.br
http->post( "register.html", data ); // id == 3
.br
.fi
.PP
and the get() request fails because the host lookup fails, then the post() request is never executed and the signals would look like this:
.PP
.nf
.br
requestStarted( 1 )
.br
requestFinished( 1, FALSE )
.br
.br
requestStarted( 2 )
.br
stateChanged( HostLookup )
.br
requestFinished( 2, TRUE )
.br
.br
done( TRUE )
.br
.br
stateChanged( Unconnected )
.br
.fi
.PP
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.
.PP
The functions currentId() and currentRequest() provide more information about the currently executing request.
.PP
The functions hasPendingRequests() and clearPendingRequests() allow you to query and clear the list of pending requests.
Constructs a TQHttp object. Subsequent requests are done by connecting to the server \fIhostname\fR on port \fIport\fR. The parameters \fIparent\fR and \fIname\fR are passed on to the TQObject constructor.
Aborts the current request and deletes all scheduled requests.
.PP
For the current request, the requestFinished() signal with the \fCerror\fR argument \fCTRUE\fR is emitted. For all other requests that are affected by the abort(), no signals are emitted.
.PP
Since this slot also deletes the scheduled requests, there are no requests left and the done() signal is emitted (with the \fCerror\fR argument \fCTRUE\fR).
Deletes all pending requests from the list of scheduled requests. This does not affect the request that is being executed. If you want to stop this this as well, use abort().
For the requests issued with get(), post() and head(), TQHttp sets the connection to be keep-alive. You can also do this using the header you pass to the request() function. TQHttp 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().
.PP
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
.PP
If you want to close the connection immediately, you have to use abort() instead.
.PP
See also stateChanged(), abort(), requestStarted(), requestFinished(), and done().
Returns the TQIODevice pointer that is used as to store the data of the HTTP request being executed. If there is no current request or if the request does not store the data to an IO device, this function returns 0.
Returns the request header of the HTTP request being executed. If the request is one issued by setHost() or closeConnection(), it returns an invalid request header, i.e. TQHttpRequestHeader::isValid() returns FALSE.
Returns the TQIODevice pointer that is used as the data source of the HTTP request being executed. If there is no current request or if the request does not use an IO device as the data source, this function returns 0.
This signal is emitted when this object reads data from a HTTP server to indicate the current progress of the download.
.PP
\fIdone\fR is the amount of data that has already arrived and \fItotal\fR 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 \fItotal\fR is 0.(If you connect to a QProgressBar, the progress bar shows a busy indicator if the total is 0).
.PP
\fBWarning:\fR \fIdone\fR and \fItotal\fR are not necessarily the size in bytes, since for large files these values might need to be" scaled" to avoid overflow.
.PP
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.
.PP
\fIdone\fR is the amount of data that has already arrived and \fItotal\fR 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 \fItotal\fR is 0.(If you connect to a QProgressBar, the progress bar shows a busy indicator if the total is 0).
.PP
\fBWarning:\fR \fIdone\fR and \fItotal\fR are not necessarily the size in bytes, since for large files these values might need to be" scaled" to avoid overflow.
.PP
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). \fIerror\fR is TRUE if an error occurred during the processing; otherwise \fIerror\fR is FALSE.
.PP
See also requestFinished(), error(), and errorString().
Returns the last error that occurred. This is useful to find out what happened when receiving a requestFinished() or a done() signal with the \fCerror\fR argument \fCTRUE\fR.
.PP
If you start a new request, the error status is reset to NoError.
Returns a human-readable description of the last error that occurred. This is useful to present a error message to the user when receiving a requestFinished() or a done() signal with the \fCerror\fR argument \fCTRUE\fR.
Sends a get request for \fIpath\fR to the server set by setHost() or as specified in the constructor.
.PP
\fIpath\fR must be an absolute path like \fC/index.html\fR or an absolute URI like http://www.trolltech.com/index.html.
.PP
If the IO device \fIto\fR is 0 the readyRead() signal is emitted every time new content data is available to read.
.PP
If the IO device \fIto\fR is not 0, the content data of the response is written directly to the device. Make sure that the \fIto\fR pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted).
.PP
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().
.PP
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
.PP
See also setHost(), post(), head(), request(), requestStarted(), requestFinished(), and done().
Sends a header request for \fIpath\fR to the server set by setHost() or as specified in the constructor.
.PP
\fIpath\fR must be an absolute path like \fC/index.html\fR or an absolute URI like http://www.trolltech.com/index.html.
.PP
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().
.PP
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
.PP
See also setHost(), get(), post(), request(), requestStarted(), requestFinished(), and done().
Sends a post request for \fIpath\fR to the server set by setHost() or as specified in the constructor.
.PP
\fIpath\fR must be an absolute path like \fC/index.html\fR or an absolute URI like http://www.trolltech.com/index.html.
.PP
The incoming data comes via the \fIdata\fR IO device.
.PP
If the IO device \fIto\fR is 0 the readyRead() signal is emitted every time new content data is available to read.
.PP
If the IO device \fIto\fR is not 0, the content data of the response is written directly to the device. Make sure that the \fIto\fR pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted).
.PP
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().
.PP
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
.PP
See also setHost(), get(), head(), request(), requestStarted(), requestFinished(), and done().
This signal is emitted when there is new response data to read.
.PP
If you specified a device in the request where the data should be written to, then this signal is \fInot\fR emitted; instead the data is written directly to the device.
.PP
The response header is passed in \fIresp\fR.
.PP
You can read the data with the readAll() or readBlock() functions
.PP
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.
.PP
See also get(), post(), request(), readAll(), readBlock(), and bytesAvailable().
Sends a request to the server set by setHost() or as specified in the constructor. Uses the \fIheader\fR as the HTTP request header. You are responsible for setting up a header that is appropriate for your request.
.PP
The incoming data comes via the \fIdata\fR IO device.
.PP
If the IO device \fIto\fR is 0 the readyRead() signal is emitted every time new content data is available to read.
.PP
If the IO device \fIto\fR is not 0, the content data of the response is written directly to the device. Make sure that the \fIto\fR pointer is valid for the duration of the operation (it is safe to delete it when the requestFinished() signal is emitted).
.PP
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().
.PP
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
.PP
See also setHost(), get(), post(), head(), requestStarted(), requestFinished(), and done().
This signal is emitted when processing the request identified by \fIid\fR has finished. \fIerror\fR is TRUE if an error occurred during the processing; otherwise \fIerror\fR is FALSE.
.PP
See also requestStarted(), done(), error(), and errorString().
Sets the HTTP server that is used for requests to \fIhostname\fR on port \fIport\fR.
.PP
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().
.PP
When the request is started the requestStarted() signal is emitted. When it is finished the requestFinished() signal is emitted.
.PP
See also get(), post(), head(), request(), requestStarted(), requestFinished(), and done().
.SH "void TQHttp::stateChanged ( int state )\fC [signal]\fR"
This signal is emitted when the state of the TQHttp object changes. The argument \fIstate\fR 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.
.PP
See also get(), post(), head(), request(), closeConnection(), state(), and State.