既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
QNetwork类
在.pro文件中增加网络组件支持
QT += network
http访问的类:QNetworkAccessManager
http请求的类:QNetworkRequest
http回复的类:QNetworkReply
使用上述三个类即可实现所有的http客户端的功能,用法可以简单概括为:
reply = manager->get(request);
reply = manager->post(request, data);
http请求
(1)选择请求method
使用QNetwofkAccessManager
类发起请求,最终调用方法根据请求的method选择,有以下几种:
QNetworkReply \*head(const QNetworkRequest &request);
QNetworkReply \*get(const QNetworkRequest &request);
QNetworkReply \*post(const QNetworkRequest &request, QIODevice \*data);
QNetworkReply \*post(const QNetworkRequest &request, const QByteArray &data);
QNetworkReply \*post(const QNetworkRequest &request, QHttpMultiPart \*multiPart);
QNetworkReply \*put(const QNetworkRequest &request, QIODevice \*data);
QNetworkReply \*put(const QNetworkRequest &request, const QByteArray &data);
QNetworkReply \*put(const QNetworkRequest &request, QHttpMultiPart \*multiPart);
QNetworkReply \*deleteResource(const QNetworkRequest &request);
QNetworkReply \*sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QIODevice \*data = Q_NULLPTR);
(2)构造请求uri和query
QNetworkRequest request;
QUrlQuery urlQuery;
QString urlString;
QUrl url;
/\* url \*/
urlString.append("http://");
urlString.append("www.baidu.com");
urlString.append("/echo");
url.setUrl(urlString);
/\* query \*/
urlQuery.addQueryItem("id", "123456");
urlQuery.addQueryItem("time", "2019-01-01 10:30:00");
url.setQuery(urlQuery);
request.setUrl(url);
(3)追加/修改请求头
QNetworkRequest request;
request.setRawHeader("User-Agent", "qhttpc 1.0");
request.setRawHeader("Authorization", "Basic YWRtaW46YWRtaW4=");
(3)追加请求body
在使用post或put方式的请求时,需要追加请求负载数据时,将负载数据带入请求
manager->post(request, data);
http回复
(1)接收回复状态码
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (statusCode.isValid()) {
qDebug() << "recv finished: " << statusCode.toInt();
}
int httpCode = statusCode.toInt();
(2)接收回复头
QList<QByteArray> list = reply->rawHeaderList();
for (int i = 0; i < list.size(); i++) {
qDebug() << "header: " << list.at(i).data() << " " << reply->rawHeader(list.at(i));
}
(3)接收回复数据
response = reply->readAll();
http同步接收数据
增加定时器,设置超时时间为30秒,当reply接收完成或超时后,eventLoop.exec()执行退出,继续处理数据处理操作
QEventLoop eventLoop;
QTimer timer;
connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
timer.start(30 \* 1000);
eventLoop.exec(); // block until finish
if (timer.isActive()) {
timer.stop();
} else {
disconnect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
reply->abort();
reply->deleteLater();
return -1;
}
http用户认证
http请求的认证信息通过增加认证头来实现。用户名和密码常用的加密方式有basic和digest加密
Basic加密头格式:
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新