Qt5 移除了QHttp是由於功能重複 用QNetworkAccessManager徹底能搞定html
新建一個控制檯應用程序緩存
看一下QNetworkAccessManager的幫助文檔this
須要添加Qt + =networkurl
新建一個類spa
.h文件3d
#ifndef DOWNPAGES_H #define DOWNPAGES_H #include<QNetworkAccessManager> #include <QObject> class DownPages : public QObject { Q_OBJECT public: explicit DownPages(QObject *parent = 0); void Do_Download(); signals: public slots: void replyFinished(QNetworkReply*); private : QNetworkAccessManager *manager; }; #endif // DOWNPAGES_H
咱們聲明瞭一個 QNetworkAccesManagercode
在.cpp裏咱們把它初始化並把它的finshed信號關聯槽htm
並判斷狀態是若是 是200也就是請求正常狀態把頁面輸出 blog
.cpp文件utf-8
#include "downpages.h" #include <QNetworkRequest> #include<QNetworkReply> #include<QDebug> #include<QTextCodec> DownPages::DownPages(QObject *parent) : QObject(parent) { manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); QUrl url; //url.setUrl("http://www.cnblogs.com/a/b"); //url.setUrl("http://www.baidu.com/"); url.setUrl("http://www.sibaieteco.com/"); //url.setUrl("www.abaibduc.com"); QNetworkRequest request(url); manager->get(request); } void DownPages::Do_Download() { } /* * 請求完成 * * */ void DownPages::replyFinished(QNetworkReply * reply) { //查看是否有錯誤 switch(reply->error()) { //若是沒有錯誤把頁面輸出 case QNetworkReply::NoError: qDebug()<<"no error"; { QTextCodec * cod=QTextCodec::codecForName("utf-8"); QString str=cod->toUnicode(reply->readAll()); qDebug()<<str; } break; case QNetworkReply::ContentNotFoundError: { QUrl failedUrl; failedUrl = reply->request().url(); int httpStatus = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt(); QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); qDebug()<<"url"<<failedUrl <<httpStatus<<" "<<reason; } break; default: { qDebug()<<"Have error"<<reply->errorString()<<" error "<<reply->error(); } } qDebug()<<"finished"; }
再在這個基礎上添加一個下載功能
.h文件
#ifndef DOWNPAGES_H #define DOWNPAGES_H #include<QNetworkAccessManager> #include <QObject> #include <QFile> class QNetworkReply; class DownPages : public QObject { Q_OBJECT public: explicit DownPages(QObject *parent = 0); void Do_Download(); signals: public slots: void replyFinished(QNetworkReply*); void readyRead(); void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); void downFinished(); private : QNetworkAccessManager *manager; QNetworkReply * managerReply; QFile *file; }; #endif // DOWNPAGES_H
.cpp文件
主要是添加 了這三個信號
managerReply = manager->get(request); connect(managerReply,SIGNAL(readyRead()),this,SLOT(readyRead())); connect(managerReply,SIGNAL(downloadProgress(qint64,qint64)), this,SLOT(downloadProgress(qint64,qint64))); connect(managerReply,SIGNAL(finished()),this,SLOT(downFinished()));
readyRead()開始下載數據
DownloadProgress顯示下載的進度
還有一個finished
#include "downpages.h" #include <QNetworkRequest> #include<QNetworkReply> #include<QDebug> #include<QTextCodec> #include<QFileInfo> #include<QDir> DownPages::DownPages(QObject *parent) : QObject(parent) { manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); QUrl url; //url.setUrl("http://www.cnblogs.com/a/b"); //url.setUrl("http://www.baidu.com/"); //url.setUrl("http://www.sibaieteco.com/"); url.setUrl("http://files.cnblogs.com/li-peng/WPFMultLanguageDemo.rar"); QNetworkRequest request(url); managerReply = manager->get(request); connect(managerReply,SIGNAL(readyRead()),this,SLOT(readyRead())); connect(managerReply,SIGNAL(downloadProgress(qint64,qint64)), this,SLOT(downloadProgress(qint64,qint64))); connect(managerReply,SIGNAL(finished()),this,SLOT(downFinished())); } void DownPages::Do_Download() { } void DownPages::replyFinished(QNetworkReply * reply) { switch(reply->error()) { case QNetworkReply::NoError: qDebug()<<"no error"; { QTextCodec * cod=QTextCodec::codecForName("utf-8"); QString str=cod->toUnicode(reply->readAll()); //qDebug()<<str; } break; case QNetworkReply::ContentNotFoundError: { QUrl failedUrl; failedUrl = reply->request().url(); int httpStatus = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt(); QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); qDebug()<<"url"<<failedUrl <<httpStatus<<" "<<reason; } break; default: { qDebug()<<"Have error"<<reply->errorString()<<" error "<<reply->error(); } } qDebug()<<"finished"; } void DownPages::readyRead() { QFileInfo fileinfo(managerReply->url().path()); QString thefileName = fileinfo.fileName().isEmpty()?"index.html":fileinfo.fileName(); QString thePath ="E:/Test/Down/"; //qDebug()<<thePath; QDir createfile; bool exist = createfile.exists(thePath); if(!exist) { createfile.mkpath(thePath); } thePath +=thefileName; file=new QFile(thePath); // if(file->open(QIODevice::Append)) // { // file->write(managerReply->readAll()); // file->flush(); // file->close(); // } if(file->open(QIODevice::Append )) { file->write(managerReply->readAll()); qDebug()<<"flu"; file->flush(); } //delete file; } void DownPages::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { qDebug()<<"Total: "<<bytesTotal<<" current received : "<<bytesReceived; } void DownPages::downFinished() { file->flush(); file->close(); delete file; }
file->open(QIODevice::Append)
枚舉值 | 描述 |
QIODevice::NotOpen |
未打開 |
QIODevice::ReadOnly |
以只讀方式打開 |
QIODevice::WriteOnly |
以只寫方式打開 |
QIODevice::ReadWrite |
以讀寫方式打開 |
QIODevice::Append |
以追加的方式打開,新增長的內容將被追加到文件末尾 |
QIODevice::Truncate |
以重寫的方式打開,在寫入新的數據時會將原有數據所有清除,遊標設置在文件開頭。 |
QIODevice::Text |
在讀取時,將行結束符轉換成 \n;在寫入時,將行結束符轉換成本地格式,例如 Win32 平臺上是 \r\n |
QIODevice::Unbuffered |
忽略緩存 |