详细代码:
get请求:
QTimer timer;
timer.setInterval(5000); // 设置超时时间
timer.setSingleShot(true); // 单次触发
if(m_pGetManager)
{
delete m_pGetManager;
m_pGetManager = nullptr;
}
m_pGetManager = new QNetworkAccessManager(this);
QString strUrl = QString("%1test/listFocusAttribute/%2")
.arg(g_strWebIp).arg(strStudyUid);
QUrl url(strUrl);
QNetworkRequest* request = new QNetworkRequest;
QSslConfiguration config;
config.setPeerVerifyMode(QSslSocket::VerifyNone);
config.setProtocol(QSsl::TlsV1SslV3);
request->setSslConfiguration(config);
request->setUrl(QUrl(url));
request->setHeader(QNetworkRequest::ContentTypeHeader, "application/json;charset=UTF-8");
request->setRawHeader("Authorization", g_strToken.toLatin1());
QNetworkReply* reply = m_pGetManager->get(*request);
QEventLoop eventLoop;
connect(&timer, &QTimer::timeout, this, &CLessionWgt::sltReqTimeOut);
connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
timer.start();
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
QByteArray data = reply->readAll();
if(QString(data).isEmpty())
{
disconnect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
emit sigTipsMsg(ERRORMSG, tr("network anomaly"));
JCTC << tr("network anomaly ") << reply->errorString();
timer.stop();
reply->abort();
reply->deleteLater();
reply = nullptr;
return;
}
QJsonParseError jsonError;
QJsonDocument json = QJsonDocument::fromJson(data, &jsonError);
if (!timer.isActive())
{
disconnect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
}
else
{
if (jsonError.error == QJsonParseError::NoError)
{
if (json.isObject())
{
QJsonObject obj = json.object();
QJsonValue Jsvalue;
if (obj.contains("code"))
{
Jsvalue = obj.value("code");
int iResCode = Jsvalue.toVariant().toInt();
if (iResCode == 0)
{
if(obj.contains("data") && obj.value("data").isArray())
{
paserAttributeInfo(obj.value("data").toArray());
}
}
else
{
emit sigTipsMsg(ERRORMSG, tr("Failed to obtain disease screening information, error code: %1").arg(iResCode));
if(obj.contains("msg"))
{
JCTC << obj.value("msg").toString();
}
}
}
}
else
{
JCTC << "json error";
}
}
else
{
emit sigTipsMsg(ERRORMSG, tr("Abnormal access to disease screening information"));
}
}
timer.stop();
reply->abort();
reply->deleteLater();
reply = nullptr;
post请求
QTimer timer;
timer.setInterval(5000); // 设置超时时间
timer.setSingleShot(true); // 单次触发
if(m_pSaveManager)
{
delete m_pSaveManager;
m_pSaveManager = nullptr;
}
m_pSaveManager = new QNetworkAccessManager(this);
QString strUrl = QString("%1carotid_crf/carotidCrfFocusAttribute/addFocusAttribute").arg(g_strWebIp);
QUrl url(strUrl);
QNetworkRequest* request = new QNetworkRequest;
QSslConfiguration config;
config.setPeerVerifyMode(QSslSocket::VerifyNone);
config.setProtocol(QSsl::TlsV1SslV3);
request->setSslConfiguration(config);
request->setUrl(QUrl(url));
request->setHeader(QNetworkRequest::ContentTypeHeader, "application/json;charset=UTF-8");
request->setRawHeader("Authorization", g_strToken.toLatin1());
QNetworkReply* reply = m_pSaveManager->post(*request, getChangedJson());
QEventLoop eventLoop;
connect(&timer, &QTimer::timeout, this, &CLessionWgt::sltReqTimeOut);
connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
timer.start();
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
QByteArray data = reply->readAll();
if(QString(data).isEmpty())
{
disconnect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
emit sigTipsMsg(ERRORMSG, tr("network anomaly"));
JCTC << tr("network anomaly ") << reply->errorString();
timer.stop();
reply->abort();
reply->deleteLater();
reply = nullptr;
return false;
}
QJsonParseError jsonError;
QJsonDocument json = QJsonDocument::fromJson(data, &jsonError);
if (!timer.isActive())
{
disconnect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
}
else
{
if (jsonError.error == QJsonParseError::NoError)
{
if (json.isObject())
{
QJsonObject obj = json.object();
QJsonValue Jsvalue;
if (obj.contains("code"))
{
Jsvalue = obj.value("code");
int iResCode = Jsvalue.toVariant().toInt();
if (iResCode == 0)
{
emit sigTipsMsg(NORMALMSG, tr("Successfully saved disease screening information"));
}
else
{
emit sigTipsMsg(ERRORMSG, tr("Failed to save disease screening information, error code: %1").arg(iResCode));
JCTC << tr("Failed to save disease screening information, error code: %1").arg(iResCode);
if(obj.contains("msg"))
{
JCTC << obj.value("msg").toString();
}
timer.stop();
reply->abort();
reply->deleteLater();
reply = nullptr;
return false;
}
}
else
{
emit sigTipsMsg(ERRORMSG, tr("Save disease screening information abnormal"));
JCTC << tr("Save disease screening information abnormal");
timer.stop();
reply->abort();
reply->deleteLater();
reply = nullptr;
return false;
}
}
else
{
JCTC << "json error";
timer.stop();
reply->abort();
reply->deleteLater();
reply = nullptr;
return false;
}
}
else
{
emit sigTipsMsg(ERRORMSG, tr("Save disease screening information abnormal"));
JCTC << tr("Save disease screening information abnormal");
timer.stop();
reply->abort();
reply->deleteLater();
reply = nullptr;
return false;
}
}
timer.stop();
reply->abort();
reply->deleteLater();
reply = nullptr;
return true;```