gpt4 book ai didi

c++ - QtCUrl post 不再有效(Linux 正常...windows 正常)

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:12 28 4
gpt4 key购买 nike

我从去年开始就在我的程序中运行这个函数(Linux 和 Windows)。

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

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

我有另一台计算机(装有 Mint 19),该程序运行平稳,但在我的计算机上(也使用 Mint 19)编译没问题,但它启动了 curl.exec(我正在使用 Qtcurl 库和内部调用 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 "";
}

最佳答案

我改变了所有的方法。

第一个函数是带有查询的 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();

}

关于c++ - QtCUrl post 不再有效(Linux 正常...windows 正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261587/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com