Qt 网络编程之HTTP通信(QNetworkRequest、QNetworkReply、NetworkAccessManager)

简介

  • Qt网络模块提供了一些类实现OSI 7层网络模型中高层的网络协议,如HTTP、FTP、SNMP等
    这些类主要为:
QNetworkRequest类:
  • 通过一个URL地址发起网络协议请求,也保存网络请求的信息,目前支持HTTP、FTP和局部文件URLs的下载或上传

The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
QNetworkRequest is part of the Network Access API and is the class holding the information necessary to send a request over the network. It contains a URL and some ancillary information that can be used to modify the request.

NetworkAccessManager类:

用于协调网络操作。在QNetworkRequest发起一个网络请求后,NetworkAccessManager负责发送网络请求,创建网络响应

QNetworkReply类:
  • 表示网络请求的响应,包含了响应的数据。

The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
The QNetworkReply class contains the data and meta data related to a request posted with QNetworkAccessManager. Like QNetworkRequest, it contains a URL and headers (both in parsed and raw form), some information about the reply’s state and the contents of the reply itself.
QNetworkReply is a sequential-access QIODevice, which means that once data is read from the object, it no longer kept by the device. It is therefore the application’s responsibility to keep this data if it needs to. Whenever more data is received from the network and processed, the readyRead() signal is emitted.
The downloadProgress() signal is also emitted when data is received, but the number of bytes contained in it may not represent the actual bytes received, if any transformation is done to the contents (for example, decompressing and removing the protocol overhead).
Even though QNetworkReply is a QIODevice connected to the contents of the reply, it also emits the uploadProgress() signal, which indicates the progress of the upload for operations that have such content.
Note: Do not delete the object in the slot connected to the error() or finished() signal. Use deleteLater().

  • 由NetworkAccessManager在发送一个网络请求后创建一个网络响应。QNetworkReply提供finished()、readyRead()、downloadProgress()可以监测网络响应的执行情况,执行相应的操作。
  • readyRead信号:有新数据到来,可以从device进行读取的时候会被发送这个信号。
  • 在error() 或 finished() 信号对应的槽函数中不能delete QNetworkReply对象,要使用deleteLater()来删除
  • QNetworkReply是QIODevice的子类,所以QNetworkReply支持流读写功能,也支持异步或同步工作

例子

QUrl qurl;
QString sUrl;
qurl.setUrl(sUrl, QUrl::TolerantMode);
QNetworkRequest request;
request.setUrl(qurl);
//发送网络请求,创建网络响应
QNetworkReply *reply=networkManager->get(request);
//关联信号与槽
connect(reply,SIGNAL(finished()),this,SLOT(on_finished()));
connect(reply,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
connect(reply,SIGNAL(downloadProgress(qint64,qint64)),
            this,SLOT(on_downloadProgress(qint64,qint64)));

//读取下载的数据
on_readyRead()
{
downloadedFile->write(reply->readAll());
}
//更新进度
on_downloadProgress(qint64 bytesRead,qint64 totalBytes)
{    
    ui->progressBar->setMaximum(totalBytes);
    ui->progressBar->setValue(bytesRead);
}
//请求结束之后
on_finished()
{
    QFileInfo fileInfo;
    fileInfo.setFile(downloadedFile->fileName());
    
    //关闭打开的网络文件,然后置空
    downloadedFile->close();
    delete downloadedFile;
    downloadedFile=Q_NULLPTR;
    
    //删除网络响应并置空
    reply->deleteLater();
    reply=Q_NULLPTR;
    
    QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("下载成功"));
 
    //如果设置了下载完成后打开,就打开文件
    if(ui->checkOpen->isChecked())
    {
        //调用缺省的应用程序软件打开下载的文件
        QDesktopServices::openUrl(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
        ui->btnDownload->setEnabled(true);
    }
}

部分内容参考自:http://www.skcircle.com/?id=972

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值