Qt中带token的Http请求

详细代码:

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;```

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星火撩猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值