Qt中带token的Http请求

Posted 星火撩猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;```

以上是关于Qt中带token的Http请求的主要内容,如果未能解决你的问题,请参考以下文章

远程http请求时,后缀带空格,请求报错问题(类似word文档标题中带空格)

JAVA中带有JSON字符串的HTTP POST请求

.NET 4.5 和 C# 中带有 HttpClient 的 HTTP HEAD 请求

http、https请求URL中带有&等特殊字符的解决方法

Spring Cloud中Feign如何统一设置验证token

Spring Cloud中Feign如何统一设置验证token