博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt学习笔记网络(一)
阅读量:5052 次
发布时间:2019-06-12

本文共 5252 字,大约阅读时间需要 17 分钟。

Qt5 移除了QHttp是因为功能重复 用QNetworkAccessManager完全能搞定

新建一个控制台应用程序

看一下QNetworkAccessManager的帮助文档

需要添加Qt + =network

新建一个类

.h文件

#ifndef DOWNPAGES_H#define DOWNPAGES_H#include
#include
class DownPages : public QObject{ Q_OBJECTpublic: explicit DownPages(QObject *parent = 0); void Do_Download();signals:public slots:void replyFinished(QNetworkReply*);private : QNetworkAccessManager *manager;};#endif // DOWNPAGES_H

我们声明了一个 QNetworkAccesManager

在.cpp里我们把它初始化并把它的finshed信号关联槽

并判断状态是如果 是200也就是请求正常状态把页面输出 

.cpp文件

 

#include "downpages.h"#include 
#include
#include
#include
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()<
request().url(); int httpStatus = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt(); QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); qDebug()<<"url"<
<
<<" "<
errorString()<<" error "<
error(); } } qDebug()<<"finished";}

再在这个基础上添加一个下载功能

.h文件

#ifndef DOWNPAGES_H#define DOWNPAGES_H#include
#include
#include
class QNetworkReply;class DownPages : public QObject{ Q_OBJECTpublic: 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 
#include
#include
#include
#include
#include
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()<
request().url(); int httpStatus = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt(); QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); qDebug()<<"url"<
<
<<" "<
errorString()<<" error "<
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()<
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: "<
<<" current received : "<
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 忽略缓存

转载于:https://www.cnblogs.com/li-peng/p/3656613.html

你可能感兴趣的文章
js-创建对象的几种方式
查看>>
JDK JRE Java虚拟机的关系
查看>>
2018.11.20
查看>>
word20161215
查看>>
dijkstra (模板)
查看>>
编译Linux驱动程序 遇到的问题
查看>>
大型分布式网站架构技术总结
查看>>
HDU 1017[A Mathematical Curiosity]暴力,格式
查看>>
[算法之美] KMP算法的直观理解
查看>>
EntityFramework 性能优化
查看>>
【ASP.NET开发】菜鸟时期的ADO.NET使用笔记
查看>>
android圆角View实现及不同版本号这间的兼容
查看>>
OA项目设计的能力③
查看>>
Cocos2d-x3.0 文件处理
查看>>
全面整理的C++面试题
查看>>
Activity和Fragment生命周期对比
查看>>
android 分辨率自适应
查看>>
查找 EXC_BAD_ACCESS 问题根源的方法
查看>>
日常报错
查看>>
list-style-type -- 定义列表样式
查看>>