【问题标题】:QtCUrl post doesn´t work anymore (Linux nok...windows ok)QtCUrl 帖子不再起作用(Linux nok...windows ok)
【发布时间】:2019-12-07 06:01:42
【问题描述】:

自去年以来,我一直在我的程序中运行此功能(Linux 和 Windows)。

现在我需要实现一个新功能,而我的新构建不再运行。

我还有其他使用 POST 的 CUrl 函数,结果相同:没有,但我的 GET 函数没问题。

我有另一台计算机(使用 Mint 19),该程序运行顺利,但在我的计算机上(也使用 Mint 19)编译很好,但它启动 curl.exec(我正在使用 Qtcurl 库并且 inside 调用了curl_easy_perform) 并且不再返回。

我安装了这个包:libcurl4-openssl-dev

可以编译我的程序(Linux 和 Windows)。该程序在 Windows 上运行。

我的问题只是 Mint19 中的新版本。

缺少什么安装?

QUrl url("https://pos-api.ifood.com.br/oauth/token");
QUrlQuery q;
q.addQueryItem("client_id", id); 
q.addQueryItem("client_secret", secret); 
q.addQueryItem("grant_type","password"); 
q.addQueryItem("username",user); 
q.addQueryItem("password",password); 

url.setQuery(q);

QtCUrl::Options opt;
opt[CURLOPT_URL] = url;
opt[CURLOPT_POST] = true;
opt[CURLOPT_FOLLOWLOCATION] = true;
opt[CURLOPT_FAILONERROR] = true;


opt[CURLOPT_SSL_VERIFYPEER]= false;  // windows

QStringList headers;
headers
    << "cache-control: no-cache"
    << "content-type: application/x-www-form-urlencoded";
opt[CURLOPT_HTTPHEADER] = headers;
val = cUrl.exec(opt);  // PROBLEM HERE!!!!

if (cUrl.lastError().isOk()) {

    bool ok;
    // json is a QString containing the JSON data
    QtJson::JsonObject result = QtJson::parse(val, ok).toMap();
    token=result["access_token"].toString();


    return token;
}
else {
    return "";
}

【问题讨论】:

    标签: c++ linux qt curl linux-mint


    【解决方案1】:

    我改变了我所有的方法。

    第一个函数是一个带有查询的 POST。

        QString iFood_getToken2(QString token, int *expira, QString id, QString secret, QString user, QString password, QString host){
        if(host!=hostname || !ifood_ativo){
            qDebug() << "iFood_getToken2 saindo...";
            return "";
        }
    
        if(*expira>IFOOD_TASK){
            *expira-=IFOOD_TASK;
           // qDebug() << "expira " << *expira;
            return token;   // token válido
        }
    
        QUrl url("https://pos-api.ifood.com.br/oauth/token");
    
        QUrlQuery q;
        q.addQueryItem("client_id", id); 
        q.addQueryItem("client_secret", secret); 
        q.addQueryItem("grant_type","password"); 
        q.addQueryItem("username",user); 
        q.addQueryItem("password",password); 
        url.setQuery(q);
    
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(int(QNetworkRequest::AlwaysNetwork)));
    
        QJsonObject json;
    
        QNetworkAccessManager nam;
    
        QNetworkReply *reply = nam.post(request, QJsonDocument(json).toJson());
    
        while (!reply->isFinished())
        {
            qApp->processEvents();
        }
    
        QByteArray response_data = reply->readAll();
        QJsonDocument jsonr = QJsonDocument::fromJson(response_data);
        reply->deleteLater();
    
        //qDebug() << "ifoodtoken2 " << jsonr["access_token"].toString();
    
        return jsonr["access_token"].toString();
    }
    

    我确实实现了这些新功能:

    GET 和 PATCH 有一个新的实现

    所以,从现在开始我不再需要使用 CUrl 库了

    QJsonDocument networkGet(QString strUrl, QString token){
        QUrl url(strUrl);
    
        QNetworkRequest request(url);
        request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(int(QNetworkRequest::AlwaysNetwork)));
    
        QString headerData = "bearer " + token;
        request.setRawHeader("Authorization", headerData.toLocal8Bit());
    
        QJsonObject json;
    
        QNetworkAccessManager nam;
    
        QNetworkReply *reply = nam.get(request);
    
        while (!reply->isFinished())
        {
            qApp->processEvents();
        }
    
        QByteArray response_data = reply->readAll();
        QJsonDocument json_response = QJsonDocument::fromJson(response_data);
        reply->deleteLater();
    
        //qDebug() << "networkGet " << json_response << reply->errorString() << headerData ;
    
        return json_response;
    
    }
    
    int networkPatch(QString strUrl, QString token, QJsonDocument json){
        QUrl url(strUrl);
    
        QNetworkRequest request(url);
        request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QVariant(int(QNetworkRequest::AlwaysNetwork)));
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    
        QString headerData = "bearer " + token;
        request.setRawHeader("Authorization", headerData.toLocal8Bit());
    
        QNetworkAccessManager nam;
    
        QByteArray * _b_arr = new QByteArray (QString(json.toJson()).toLatin1());
        QBuffer *_qbf_upload =new QBuffer (_b_arr);
        QNetworkReply *reply = nam.sendCustomRequest(request,"PATCH",_qbf_upload);
    
        while (!reply->isFinished())
        {
            qApp->processEvents();
        }
    
        QByteArray response_data = reply->readAll();
        QJsonDocument json_response = QJsonDocument::fromJson(response_data);
        reply->deleteLater();
    
        qDebug() << "networkPatch " << reply->error() << json_response << reply->errorString() << headerData ;
    
        return reply->error();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 2018-12-15
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多