头文件如下
#pragma once
#ifndef _HTTP_H_
#define _HTTP_H_
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QHttpPart>
#include <QHttpMultiPart>
#include <QUrl>
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonArray>
#include <QEventLoop>
#include <QDebug>
#include <QFile>
#include <QUrlQuery>
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, 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
源文件如下:
#include "http.h"
#include <QTimer>
#include <QFileInfo>
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()));
timer.start(1000);
_loop.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);
multiPart->append(filePart);
QNetworkAccessManager* pNetManager = new QNetworkAccessManager;
QUrl _url(url);
QNetworkRequest request(_url);
QNetworkReply* reply = pNetManager->post(request, multiPart);
multiPart->setParent(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));
}
|