HTTP即超文本传输协议,它是一种文件传输协议。这一节中我们将讲解如何利用HTTP从网站上下载文件。
下载网页:
1.manager = new QNetworkAccessManager(this); //新建QNetworkAccessManager对象
2.使用get发送请求连接
manager->get(QNetworkRequest(QUrl("http://www.yafeilinux.com"))); //发送请求
3.在请求完成时,manager会发出信号SIGNAL(finished(QNetworkReply*)),这时执行槽函数replyFinished,将下载到的数据显示出来,以html方式
ui->textBrowser->setHtml(all);
下载文件
用到的几个信号
readyRead信号在请求的数据中有新的数据响应的时候产生,什么才算新的数据,一次从外网传输过来一个什么数据包?help没讲---暂时理解刚传过来的某个包属于请求的1部分
finished信号在请求的数据全部返回时产生
downloadProgress信号在..help没讲,可能是定时发生吧
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 read() 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.
This signal is emitted to indicate the progress of the download part of this network request, if there's any. If there's no download associated with this request, this signal will be emitted once with 0 as the value of both bytesReceived and bytesTotal.
The bytesReceived parameter indicates the number of bytes received, while bytesTotal indicates the total number of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, bytesTotal will be -1.
The download is finished when bytesReceived is equal to bytesTotal. At that time, bytesTotal will not be -1.
Note that the values of both bytesReceived and bytesTotal may be different from size(), the total number of bytes obtained through read() or readAll(), or the value of the header(ContentLengthHeader). The reason for that is that there may be protocol overhead or the data may be compressed during the download.
This signal is emitted when the reply has finished processing. After this signal is emitted, there will be no more updates to the reply's data or metadata.
Unless close() has been called, the reply will be still be opened for reading, so the data can be retrieved by calls to read() or readAll(). In particular, if no calls to read() were made as a result of readyRead(), a call to readAll() will retrieve the full contents in a QByteArray.
This signal is emitted in tandem with QNetworkAccessManager::finished() where that signal's reply parameter is this object.
Note: Do not delete the object in the slot connected to this signal. Use deleteLater().
You can also use isFinished() to check if a QNetworkReply has finished even before you receive the finished() signal.
1.
manager = new QNetworkAccessManager(this); //新建QNetworkAccessManager对象
2.
file = new QFile(fileName);//创建文件
reply = manager->get(QNetworkRequest(url));//请求连接
3.
在readyRead信号时,将新的数据写入文件
在downloadProgress信号时,更新进度条
在finished信号时,释放各个资源
可见下载网页和文件都是manager->get来完成的。网页部分是将数据显示在textBrowser,文件部分是将数据存入文件。
并且网页请求可以像下载文件那样每1小部分下载完就显示出来,即在readyRead信号时将收到的部分数据显示出来
下图是wireshark在此次下载网页程序执行时的抓包结果,可以看到http是封装在tcp里面