1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include "httpreq.h"
- #include "maindlg.h"
- #include <QMessageBox>
- HttpReq::HttpReq(QObject *parent)
- : QObject{parent}
- {
- m_pMainDlg = static_cast<MainDlg *>(parent);
- init();
- }
- void HttpReq::init()
- {
- m_pNetworkManager = new QNetworkAccessManager(this);
- return;
- }
- quint8 HttpReq::configNetworkParam(QString strHostAddress, QString strPort)
- {
-
- if (strHostAddress.length() <= 0 || strPort.length() <= 0)
- {
- return 1;
- }
-
- m_strBaseUrl = strHostAddress + ":" + strPort + "/pcapi/bparam/";
- return 0;
- }
- QString HttpReq::sendPostReq(QString strApiName, QJsonObject jsonObject)
- {
- QString strBaseUrl = "";
- QNetworkReply *reply = NULL;
- QEventLoop loop;
-
- if (strApiName.length() <= 0)
- {
- return QString();
- }
-
-
- strBaseUrl = m_strBaseUrl + strApiName;
- QUrl hostUrl(strBaseUrl);
- QNetworkRequest request(hostUrl);
-
- request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
-
- QJsonDocument jsonDoc(jsonObject);
- QByteArray jsonData = jsonDoc.toJson();
-
- reply = m_pNetworkManager->post(request, jsonData);
-
- QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
-
- loop.exec();
-
- if (reply->error() == QNetworkReply::NoError)
- {
-
- return QString("上传数据成功");
- }
- else
- {
- return QString("上传数据失败:") + QString::fromUtf8(reply->readAll());
- }
- }
|