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響應內容緩衝中讀取的數據大小。

















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