使用到中重要的类,做个简单的介绍
QNetworkAccessManager:这个类是QT帮我们封装好的工具类,主要可以用来发送Http请求QNetworkReply:这个类主要用来监听发送的请求,并得到请求的响应结果QHttpMultiPart:这个类主要用于封装post请求的二进制参数的数据QEventLoop:这个类是一个控制事件QJsonDocument:这个类用于解析JSON
头文件:
#ifndef HTTPTEST_H
#define HTTPTEST_H
#include <QObject>
#include <QDebug>
#include <QHttpMultiPart>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMetaObject>
#include <QEventLoop>
#include <QJsonDocument> //以下是json数据传送所需头文件
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>
#include <QFile>
class myHttp : public QObject
{
Q_OBJECT
public:
explicit myHttp(QObject *parent = nullptr);
void sendGetRequest();
void sendPostRequest();
QString resultGet;
QString resultPost;
public slots:
void receiveReplyGet(QNetworkReply *reply);
void receiveReplyPost(QNetworkReply *reply);
};
#endif // HTTPTEST_H
cpp文件
#include "myHttp.h"
myHttp::myHttp(QObject *parent) : QObject(parent)
{
}
void myHttp::sendGetRequest()
{
qDebug()<<"get方法被执行";
QNetworkRequest request1;
QNetworkAccessManager nam;
connect(&nam, &QNetworkAccessManager::finished, this, &myHttp::receiveReplyGet);
//get与post的请求方式有所不同,get是在接口名后添加 ? 和传输的数据(type)
request1.setUrl(QUrl("http://localhost:8080/dish/page?hello=666&name=hjl&age=18"));
request1.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json;charset=utf-8"));
QString token = "xxxxxxxxxxxxxxxxxxxxxxxx";
QString token_headerData = token;
request1.setRawHeader("token",token_headerData.toLocal8Bit());
QNetworkReply *reply = nam.get(request1); //get请求头
//开启事件循环,直到请求完成
QEventLoop loop;
connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
loop.exec();
}
void myHttp::receiveReplyGet(QNetworkReply *reply)
{
qDebug()<<"得到get方法的返回值";
// 获取状态码
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// 获取响应数据
QString reply_data = reply->readAll();
qDebug()<<reply_data<<status_code;
resultGet=reply_data;
//解析json字符串
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply_data.toUtf8());
QJsonObject jsonObj = jsonDoc.object();
QString date = jsonObj["date"].toString();
int code = jsonObj["code"].toInt();
QString msg = jsonObj["mes"].toString();
qDebug()<<reply_data<<status_code<<date<<msg<<code;
}
void myHttp::sendPostRequest()
{
qDebug()<<"post方法被执行";
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
//封装文本参数
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=text"));
textPart.setBody("text");
//封装文件参数
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=file; filename=output.wav"));
QString filename="F:\\output.wav";
QFile file(filename);
QByteArray bytes;
if (!file.exists()) {
qDebug() << "文件不存在";
return;
}
if (file.open(QFile::ReadOnly)) {
bytes = file.readAll();
qDebug() << "状态:读取文件成功";
}
file.close();
filePart.setBody(bytes);
qDebug()<<bytes.size();
//将封装到的参数加入到这个对象中
multiPart->append(textPart);
multiPart->append(filePart);
QUrl url("http://localhost:8080/common/upload?age=18&hobby=football");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data; boundary="+multiPart->boundary()));
QNetworkAccessManager manager;
connect(&manager, &QNetworkAccessManager::finished, this, &myHttp::receiveReplyPost);
QNetworkReply *reply = manager.post(request, multiPart);
//开启事件循环,直到请求完成
QEventLoop loop;
connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
loop.exec();
}
void myHttp::receiveReplyPost(QNetworkReply *reply)
{
qDebug()<<"post方法返回";
// 获取状态码
int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// 获取响应数据
QString reply_data = reply->readAll();
resultPost=reply_data;
//解析json字符串
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply_data.toUtf8());
QJsonObject jsonObj = jsonDoc.object();
QString date = jsonObj["data"].toString();
int code = jsonObj["code"].toInt();
QString msg = jsonObj["msg"].toString();
qDebug()<<reply_data<<status_code<<date<<msg<<code;
}
效果:
最后,如有不足欢迎指正,如有疑问,欢迎评论