头文件如下
#pragma once#ifndef _HTTP_H_
#define _HTTP_H_#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include typedef struct _HTPPError
{
int dataerrorcode;
int fileerrorcode;
}HTPPError;
class Http : public QObject
{
Q_OBJECT public:
Q_INVOKABLE void SendData(const QString& url, QJsonObject jsonObj);
//Q_INVOKABLE void SendFile(const QString& url, QFile *file, QString FileName);
Q_INVOKABLE void SendFile(const QString& url, const QString strFilePath);
HTPPError GetHTPPError() { return Error;
} public:
const QString urldata = "http://192.168.1.110:82/prod-api/dafen/cableAuto/upload/dafen-data";
const QString urlpic = "http://192.168.1.110:82/prod-api/dafen/cableAuto/upload/cable-picture?cableId=1&desc=";
const QString urlpdf = "http://192.168.1.110:82/prod-api/dafen/cablePdf/upload/cable-pdf?cableId=1";
private:
HTPPError Error;
};
#endif
【学习QT|QT的http封装类】源文件如下:
#include "http.h"
#include
#include void Http::SendData(const QString& url, QJsonObject jsonObj)
{
QJsonDocument document;
document.setObject(jsonObj);
QByteArray content = document.toJson(QJsonDocument::Compact);
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
request.setUrl(url);
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply* reply = manager->post(request, content);
QEventLoop _loop;
QTimer timer;
connect(reply, SIGNAL(finished()), &_loop, SLOT(quit()));
connect(&timer, SIGNAL(timeout()), &_loop, SLOT(quit()));
// eventloop用于阻塞等待消息,并结合timer进行超时处理
timer.start(1000);
_loop.exec();
// 这里的eventloop只是阻塞此段,并不会阻塞其他块的代码,比如我不加定时器,让这个eventloop一直exec,还是可以出发相关的槽函数 int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
Error.dataerrorcode = status_code;
if (timer.isActive())
{
timer.stop();
} _loop.deleteLater();
timer.deleteLater();
// 释放资源
reply->abort();
}
void Http::SendFile(const QString& url,const QString strFilePath)
{
QEventLoop loop;
QFile *file = new QFile(strFilePath);
file->open(QIODevice::ReadOnly);
QHttpMultiPart*multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart filePart;
QFileInfo upload_file_info(strFilePath);
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
//这里一般这样写
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data;
name=\"file\";
filename=\"" + upload_file_info.fileName() + "\""));
filePart.setBodyDevice(file);
file->setParent(multiPart);
//delte file with the multiPart
multiPart->append(filePart);
QNetworkAccessManager* pNetManager = new QNetworkAccessManager;
QUrl _url(url);
QNetworkRequest request(_url);
QNetworkReply* reply = pNetManager->post(request, multiPart);
multiPart->setParent(reply);
//delte multiPart with the reply
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
pNetManager->deleteLater();
}
调用方法如下:
先定义一个QJsonObject,然后插入QString(项目代码里面粘贴复制的);
QJsonObject jsonObj;
jsonObj.insert("cableId", QString::number(cabelid));
jsonObj.insert("1", result.conductor_size);
jsonObj.insert("2", result.core_pencil_head_size);
jsonObj.insert("3", result.outside_pencil_head_size);
jsonObj.insert("4", result.inner_semiconducting_size);
jsonObj.insert("5", result.main_insulation_deviation);
jsonObj.insert("6", result.core_outside_semiconducting_end);
jsonObj.insert("7", result.core_top_2_outside_pencil_head_distance);
jsonObj.insert("8", result.conductor_deformation);
jsonObj.insert("9", result.core_top_uneven);
jsonObj.insert("10", result.conductor_section_inclination);
jsonObj.insert("11", result.outside_semiconducting_size);
jsonObj.insert("12", result.outer_diameter_difference_200_265);
jsonObj.insert("13", result.insulation_core_size_18);
jsonObj.insert("14", result.max_con1);
jsonObj.insert("15", result.max_con2);
jsonObj.insert("16", result.max_con3);
void Collection::Send()
{
http->SendData(http->urldata, getJson());
//传输数据
QMessageBox::about(this, tr("Error"), "http数据编码为:"+QString::number(http->GetHTPPError().dataerrorcode));
SendPic(GetVTKQPixmap(ui.qvtkWidget2));
SendPic(GetVTKQPixmap(ui.qvtkWidget3));
SendPic(GetVTKQPixmap(ui.qvtkWidget4));
SendPic(GetVTKQPixmap(ui.qvtkWidget5));
}
推荐阅读
- C++ STL Merge的用法
- c++|虚函数和多态性
- CCF-CSP|【手把手刷CCF】201809-2-买菜100分(含详细注释)
- OJ|阶乘分解 kkmd66
- 面经|美团后端一二面c++
- 图书管理系统简易版C++
- 极客日报|称钉钉将上线“下班勿扰”功能;苹果发生大规模网络宕机;.NET 7 Preview 2发布|极客头条
- 蓝桥杯|acwing 1113. 红与黑(蓝桥杯)
- C语言与C++编程|C语言学习方法、学习平台及项目推荐