3
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.1.4-harald-1150713676011/qt-x11-opensource-src-4.1.4/src/network/qhttp.cpp -->
6
<!-- /tmp/qt-4.1.4-harald-1150713676011/qt-x11-opensource-src-4.1.4/src/network/qhttp.cpp -->
5
<!-- /tmp/qt-4.2.0-qt-1159540607313/qt-x11-opensource-src-4.2.0/src/network/qhttp.cpp -->
8
<title>Qt 4.1: QHttp Class Reference</title>
9
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
10
a:link { color: #004faf; text-decoration: none }
11
a:visited { color: #672967; text-decoration: none }
12
td.postheader { font-family: sans-serif }
13
tr.address { font-family: sans-serif }
14
body { background: #ffffff; color: black; }</style>
7
<title>Qt 4.2: QHttp Class Reference</title>
8
<link href="classic.css" rel="stylesheet" type="text/css" />
17
11
<table border="0" cellpadding="0" cellspacing="0" width="100%">
19
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
13
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
20
14
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
21
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QHttp Class Reference<br /><sup><sup>[<a href="qtnetwork.html">QtNetwork</a> module]</sup></sup></h1>
15
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">QHttp Class Reference<br /><sup><sup>[<a href="qtnetwork.html">QtNetwork</a> module]</sup></sup></h1>
22
16
<p>The QHttp class provides an implementation of the HTTP protocol. <a href="#details">More...</a></p>
23
<pre>#include <QHttp></pre><p>Inherits <a href="qobject.html">QObject</a>.</p>
17
<pre> #include <QHttp></pre><p>Inherits <a href="qobject.html">QObject</a>.</p>
24
18
<p><b>Note:</b> All the functions in this class are <a href="threads.html#reentrant">reentrant</a>.</p>
26
20
<li><a href="qhttp-members.html">List of all members, including inherited members</a></li>
104
98
<p>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.</p>
105
99
<p>The operations that can be scheduled (they are called "requests" in the rest of the documentation) are the following: <a href="qhttp.html#setHost">setHost</a>(), <a href="qhttp.html#get">get</a>(), <a href="qhttp.html#post">post</a>(), <a href="qhttp.html#head">head</a>() and <a href="qhttp.html#request">request</a>().</p>
106
100
<p>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 <a href="qhttp.html#requestStarted">requestStarted</a>() signal with the identifier is emitted and when the request is finished, the <a href="qhttp.html#requestFinished">requestFinished</a>() signal is emitted with the identifier and a bool that indicates if the request finished with an error.</p>
107
<p>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 <tt>http://www.trolltech.com/index.html</tt>):</p>
108
<pre> QHttpRequestHeader header("GET", "/index.html");
109
header.setValue("Host", "www.trolltech.com");
110
http->setHost("www.trolltech.com");
111
http->request(header);</pre>
101
<p>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 <tt>http:<span class="comment">//www.trolltech.com/index.html</span></tt>):</p>
102
<pre> QHttpRequestHeader header("GET", "/index.html");
103
header.setValue("Host", "www.trolltech.com");
104
http->setHost("www.trolltech.com");
105
http->request(header);</pre>
112
106
<p>For the common HTTP requests <tt>GET</tt>, <tt>POST</tt> and <tt>HEAD</tt>, QHttp provides the convenience functions <a href="qhttp.html#get">get</a>(), <a href="qhttp.html#post">post</a>() and <a href="qhttp.html#head">head</a>(). 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:</p>
113
<pre> http->setHost("www.trolltech.com"); // id == 1
114
http->get("/index.html"); // id == 2</pre>
107
<pre> http->setHost("www.trolltech.com"); <span class="comment">// id == 1</span>
108
http->get("/index.html"); <span class="comment">// id == 2</span></pre>
115
109
<p>For this example the following sequence of signals is emitted (with small variations, depending on network traffic, etc.):</p>
116
<pre> requestStarted(1)
117
requestFinished(1, false)
120
stateChanged(Connecting)
121
stateChanged(Sending)
122
dataSendProgress(77, 77)
123
stateChanged(Reading)
124
responseHeaderReceived(responseheader)
125
dataReadProgress(5388, 0)
126
readyRead(responseheader)
127
dataReadProgress(18300, 0)
128
readyRead(responseheader)
129
stateChanged(Connected)
130
requestFinished(2, false)
134
stateChanged(Closing)
135
stateChanged(Unconnected)</pre>
110
<pre> requestStarted(1)
111
requestFinished(1, false)
114
stateChanged(Connecting)
115
stateChanged(Sending)
116
dataSendProgress(77, 77)
117
stateChanged(Reading)
118
responseHeaderReceived(responseheader)
119
dataReadProgress(5388, 0)
120
readyRead(responseheader)
121
dataReadProgress(18300, 0)
122
readyRead(responseheader)
123
stateChanged(Connected)
124
requestFinished(2, false)
128
stateChanged(Closing)
129
stateChanged(Unconnected)</pre>
136
130
<p>The <a href="qhttp.html#dataSendProgress">dataSendProgress</a>() and <a href="qhttp.html#dataReadProgress">dataReadProgress</a>() signals in the above example are useful if you want to show a <a href="qprogressbar.html">progressbar</a> 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 <a href="qprogressbar.html">QProgressBar</a> a total of 0 results in a busy indicator.)</p>
137
131
<p>When the response header is read, it is reported with the <a href="qhttp.html#responseHeaderReceived">responseHeaderReceived</a>() signal.</p>
138
132
<p>The <a href="qhttp.html#readyRead">readyRead</a>() signal tells you that there is data ready to be read. The amount of data can then be queried with the <a href="qhttp.html#bytesAvailable">bytesAvailable</a>() function and it can be read with the <a href="qhttp.html#read">read</a>() or <a href="qhttp.html#readAll">readAll</a>() functions.</p>
139
133
<p>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.</p>
140
134
<p>For example, if you have the following sequence of requests</p>
141
<pre> http->setHost("www.foo.bar"); // id == 1
142
http->get("/index.html"); // id == 2
143
http->post("register.html", data); // id == 3</pre>
135
<pre> http->setHost("www.foo.bar"); <span class="comment">// id == 1</span>
136
http->get("/index.html"); <span class="comment">// id == 2</span>
137
http->post("register.html", data); <span class="comment">// id == 3</span></pre>
144
138
<p>and the <a href="qhttp.html#get">get</a>() request fails because the host lookup fails, then the <a href="qhttp.html#post">post</a>() request is never executed and the signals would look like this:</p>
145
<pre> requestStarted(1)
146
requestFinished(1, false)
149
stateChanged(HostLookup)
150
requestFinished(2, true)
154
stateChanged(Unconnected)</pre>
139
<pre> requestStarted(1)
140
requestFinished(1, false)
143
stateChanged(HostLookup)
144
requestFinished(2, true)
148
stateChanged(Unconnected)</pre>
155
149
<p>You can then get details about the error with the <a href="qhttp.html#error">error</a>() and <a href="qhttp.html#errorString">errorString</a>() 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 <a href="qhttpresponseheader.html#statusCode">status code</a> of the response header.</p>
156
150
<p>The functions <a href="qhttp.html#currentId">currentId</a>() and <a href="qhttp.html#currentRequest">currentRequest</a>() provide more information about the currently executing request.</p>
157
151
<p>The functions <a href="qhttp.html#hasPendingRequests">hasPendingRequests</a>() and <a href="qhttp.html#clearPendingRequests">clearPendingRequests</a>() allow you to query and clear the list of pending requests.</p>
158
<p>The <a href="network-http.html">HTTP</a> example illustrates how to write HTTP clients using QHttp.</p>
159
<p>See also <a href="qftp.html">QFtp</a>.</p>
152
<p>See also <a href="qftp.html">QFtp</a>, <a href="network-http.html">HTTP Example</a>, and <a href="network-torrent.html">Torrent Example</a>.</p>
161
154
<h2>Member Type Documentation</h2>
162
155
<h3 class="fn"><a name="Error-enum"></a>enum QHttp::Error</h3>
260
253
<p>See also <a href="qhttp.html#clearPendingRequests">clearPendingRequests</a>(), <a href="qhttp.html#currentId">currentId</a>(), and <a href="qhttp.html#currentRequest">currentRequest</a>().</p>
261
254
<h3 class="fn"><a name="head"></a>int QHttp::head ( const <a href="qstring.html">QString</a> & <i>path</i> )</h3>
262
255
<p>Sends a header request for <i>path</i> to the server set by <a href="qhttp.html#setHost">setHost</a>() or as specified in the constructor.</p>
263
<p><i>path</i> must be an absolute path like <tt>/index.html</tt> or an absolute URI like <tt>http://www.trolltech.com/index.html</tt>.</p>
256
<p><i>path</i> must be an absolute path like <tt>/index.html</tt> or an absolute URI like <tt>http:<span class="comment">//www.trolltech.com/index.html</span></tt>.</p>
264
257
<p>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 <a href="qhttp.html#requestStarted">requestStarted</a>() and <a href="qhttp.html#requestFinished">requestFinished</a>().</p>
265
258
<p>When the request is started the <a href="qhttp.html#requestStarted">requestStarted</a>() signal is emitted. When it is finished the <a href="qhttp.html#requestFinished">requestFinished</a>() signal is emitted.</p>
266
259
<p>See also <a href="qhttp.html#setHost">setHost</a>(), <a href="qhttp.html#get">get</a>(), <a href="qhttp.html#post">post</a>(), <a href="qhttp.html#request">request</a>(), <a href="qhttp.html#requestStarted">requestStarted</a>(), <a href="qhttp.html#requestFinished">requestFinished</a>(), and <a href="qhttp.html#done">done</a>().</p>
320
313
<h3 class="fn"><a name="setProxy"></a>int QHttp::setProxy ( const <a href="qstring.html">QString</a> & <i>host</i>, int <i>port</i>, const <a href="qstring.html">QString</a> & <i>username</i> = QString(), const <a href="qstring.html">QString</a> & <i>password</i> = QString() )</h3>
321
314
<p>Enables HTTP proxy support, using the proxy server <i>host</i> on port <i>port</i>. <i>username</i> and <i>password</i> can be provided if the proxy server requires authentication.</p>
323
<pre> void Ticker::getTicks()
325
http = new QHttp(this);
326
connect(http, SIGNAL(done(bool)), this, SLOT(showPage()));
327
http->setProxy("proxy.example.com", 3128);
328
http->setHost("ticker.example.com");
329
http->get("/ticks.asp");
316
<pre> void Ticker::getTicks()
318
http = new QHttp(this);
319
connect(http, SIGNAL(done(bool)), this, SLOT(showPage()));
320
http->setProxy("proxy.example.com", 3128);
321
http->setHost("ticker.example.com");
322
http->get("/ticks.asp");
332
void Ticker::showPage()
334
display(http->readAll());
325
void Ticker::showPage()
327
display(http->readAll());
336
329
<p><a href="qhttp.html">QHttp</a> supports non-transparent web proxy servers only, such as the Squid Web proxy cache server (from <a href="http://www.squid.org/">http://www.squid.org/</a>). For transparent proxying, such as SOCKS5, use <a href="qnetworkproxy.html">QNetworkProxy</a> instead.</p>
337
330
<p>See also <a href="qftp.html#setProxy">QFtp::setProxy</a>().</p>
338
331
<h3 class="fn"><a name="setSocket"></a>int QHttp::setSocket ( <a href="qtcpsocket.html">QTcpSocket</a> * <i>socket</i> )</h3>
352
345
<p>This signal is emitted when the state of the <a href="qhttp.html">QHttp</a> object changes. The argument <i>state</i> is the new state of the connection; it is one of the <a href="qhttp.html#State-enum">State</a> values.</p>
353
346
<p>This usually happens when a request is started, but it can also happen when the server closes the connection or when a call to <a href="qhttp.html#close">close</a>() succeeded.</p>
354
347
<p>See also <a href="qhttp.html#get">get</a>(), <a href="qhttp.html#post">post</a>(), <a href="qhttp.html#head">head</a>(), <a href="qhttp.html#request">request</a>(), <a href="qhttp.html#close">close</a>(), <a href="qhttp.html#state">state</a>(), and <a href="qhttp.html#State-enum">State</a>.</p>
349
<h2>Member Function Documentation</h2>
350
<h3 class="fn"><a name="readBlock"></a><a href="qtglobal.html#qint64-typedef">qint64</a> QHttp::readBlock ( char * <i>data</i>, <a href="qtglobal.html#quint64-typedef">quint64</a> <i>maxlen</i> )</h3>
351
<p>Use <a href="qhttp.html#read">read</a>() instead.</p>
355
352
<p /><address><hr /><div align="center">
356
353
<table width="100%" cellspacing="0" border="0"><tr class="address">
357
354
<td width="30%">Copyright © 2006 <a href="trolltech.html">Trolltech</a></td>
358
355
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
359
<td width="30%" align="right"><div align="right">Qt 4.1.4</div></td>
356
<td width="30%" align="right"><div align="right">Qt 4.2.0</div></td>
360
357
</tr></table></div></address></body>