Qt类Qhttp

QHttp是对HTTP协议的封装。该类提供了直观的接口,让你可以使用HTTP协议下载或者上传数据。但是在你的程序中,我们推荐使用

QNetworkAccessManager 和 QNetworkReply类,因为这两个类更简洁,也更强大,更加贴近协议。

QHttp类异步执行,所以函数都是非阻塞的。如果调用的操作函数没有执行,它也立即返回,操作会被调度,过一会就会执行。

当执行完之后,相关信号会被发送,以报告操作结果。信号和槽这种机制依赖于事件循环。

可以被调度的操作(这种操作在以后的文档中叫做请求)是:setHost(), get(), post(), head()和 request()。

所有请求都可以返回一个特有的标识,让你可以跟踪执行的请求。当一个请求开始执行时,带有标识的requestStarted()信号就会被发送;当

一个请求完成时,带标识的requestFinished()信号会被发送,一个bool值,指明了这个请求在完成时是否带有错误。

要进行一次HTTP请求,你得设置合适的HTTP头信息。下面的例子演示了如何从Qt站点请求一个主页。
 QHttpRequestHeader header("GET", QUrl::toPercentEncoding("/index.html"));
 header.setValue("Host", "qt.nokia.com");
 http->setHost("qt.nokia.com");
 http->request(header);

下面是以上代码在执行时的信号发送序列(取决于读者的网络状态,可能会有小的变化)

 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)


上例中的信号dataSendProgress() 和dataReadProgress()很有用,通过他们,你可以在进度条里展示下载执行进度。这两信号的第二个参数是

数据总大小,不过一般情况下不可能知道数据总量,这个时候设置为0。(如果要把这个值关联到一个进度条控件,0表示繁忙。)

HTTP响应头得到后,responseHeaderReceived()信号被发送。

readyRead()信号告诉你,数据可以读出来了。数据大小可通过函数bytesAvailable()得到,read() 或者是 readAll()则可以读取数据。

如果在执行一个操作时,发生了错误,那么在操作队列中,剩下的操作不会被调度执行,这些操作也不会产生信号。如下,请求队列:
 http->setHost("www.foo.bar");       // id == 1
 http->get("/index.html");           // id == 2
 http->post("register.html", data);  // id == 3

这里我们设置的host不存在,所以get()请求失败,接着post()请求也不会执行。产生的信号如下:
 requestStarted(1)
 requestFinished(1, false)

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

 done(true)

 stateChanged(Unconnected)

你可以通过这两个函数获取详细的错误信息:error() 和errorString()。注意你仅仅能查到异常信息,比如网络连接失败。如果服务器响应HTTP错

误状态,比如404这样的,这其实只是一个很普通HTTP响应,而不是错误,不能通过上面两个函数得到信息。

函数currentId() 和currentRequest() 可以提供很多当前所执行请求的很多信息。

函数hasPendingRequests() 和clearPendingRequests()允许你查看和清除当前等待请求列表。



成员类型文档
enum QHttp::ConnectionMode
该枚举用于指定所使用的连接模式
QHttp::ConnectionModeHttp    0    这是一个常规的HTTP连接
QHttp::ConnectionModeHttps    1    这是一个使用HTTPS协议的连接,该连接会被加密

当使用HTTPS时,注意对SSL错误信号进行处理。


enum QHttp::Error
该枚举标识会出现的错误
QHttp::NoError            0    No error occurred.
QHttp::HostNotFound        2    The host name lookup failed.
QHttp::ConnectionRefused    3    The server refused the connection.
QHttp::UnexpectedClose        4    The server closed the connection unexpectedly.
QHttp::InvalidResponseHeader    5    The server sent an invalid response header.
QHttp::WrongContentLength    6    The client could not read the content correctly because an error with respect to the

content length occurred.
QHttp::Aborted            7    The request was aborted with abort().
QHttp::ProxyAuthenticationRequiredError    9    QHttp is using a proxy, and the proxy server requires authentication to

establish a connection.
QHttp::AuthenticationRequiredError    8    The web server requires authentication to complete the request.
QHttp::UnknownError            1    An error other than those specified above occurred.


enum QHttp::State
该枚举指定客户端的状态
QHttp::Unconnected    0    There is no connection to the host.
QHttp::HostLookup    1    A host name lookup is in progress.
QHttp::Connecting    2    An attempt to connect to the host is in progress.
QHttp::Sending    3    The client is sending its request to the server.
QHttp::Reading    4    The client's request has been sent and the client is reading the server's response.
QHttp::Connected    5    The connection to the host is open, but the client is neither sending a request, nor waiting

for a response.
QHttp::Closing    6    The connection is closing down, but is not yet closed. (The state will be Unconnected when the

connection is closed.)


成员函数文档

QHttp::QHttp ( QObject * parent = 0 )

Constructs a QHttp object. The parent parameter is passed on to the QObject constructor.
QHttp::QHttp ( const QString & hostName, quint16 port = 80, QObject * parent = 0 )

Constructs a QHttp object. Subsequent requests are done by connecting to the server hostName on port port.

The parent parameter is passed on to the QObject constructor.

See also setHost().
QHttp::QHttp ( const QString & hostName, ConnectionMode mode, quint16 port = 0, QObject * parent = 0 )

Constructs a QHttp object. Subsequent requests are done by connecting to the server hostName on port port using the

connection mode mode.

If port is 0, it will use the default port for the mode used (80 for Http and 443 for Https).

The parent parameter is passed on to the QObject constructor.

See also setHost().
QHttp::~QHttp () [virtual]

Destroys the QHttp object. If there is an open connection, it is closed.


void QHttp::abort () [slot]
终止当前请求,删除所有未执行的请求。
对于当前正在执行的请求,requestFinished()信号被发送,该信号发送时,参数error为true。其他请求则不会有信号产生。由于该槽删除了

所有请求,所以done()信号会被发送(参数error为true)。

void QHttp::authenticationRequired ( const QString & hostname, quint16 port, QAuthenticator * authenticator ) [signal]
如果hostname参数和port参数指定的地址需要需要授权信息,这个信号就会被发送。在你连接的信号处理槽,你可以将授权信息填充到参数

authenticator中。
Note: It is not possible to use a QueuedConnection to connect to this signal, as the connection will fail if the

authenticator has not been filled in with new information when the signal returns.(这段未看明白)


qint64 QHttp::bytesAvailable () const
返回可以从HTTP响应内容缓冲中读取的数据大小。

















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章