gpt4 book ai didi

c++ - QNetworkRequest 返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:46 27 4
gpt4 key购买 nike

我必须使用 post 到 google 脚本(见下文),我希望在脚本的末尾收到反馈,表明一切正常。

function doPost(e){
var idm = e.parameter.idm;
var sheet_purchaseHistory = SpreadsheetApp.getActive().getSheetByName('purchaseHistory');
var date = new Date();
var time = Utilities.formatDate(date, 'Asia/Tokyo', 'yyyy/MM/dd HH:mm:ss');
sheet_purchaseHistory.getRange(sheet_purchaseHistory.getLastRow()+1, 1).setValue(time);
sheet_purchaseHistory.getRange(sheet_purchaseHistory.getLastRow(), 2).setValue(mailadress);
return ContentService.createTextOutput(JSON.stringify({'status': 'success'})).setMimeType(ContentService.MimeType.JSON);
}

在我的 cpp 代码中我有:

标题(.h)

QNetworkRequest request;
QNetworkAccessManager *manager;
QByteArray ba;
QNetworkReply* reply;

构造函数:

manager = new QNetworkAccessManager(this);  // I tried with and without parsing the (this)
request.setUrl(QUrl("https://script.google.com/macros/s/AKfycbzhHBpZ8jMfa2bwE_A0lQJNqOzVl894dcjkch_-0mNC6EX9usn1/exec"));

函数内部:

manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrlQuery postData;
postData.addQueryItem("idm", "string");
QNetworkReply *rp = manager->post(request,postData.toString(QUrl::FullyEncoded).toUtf8());
QEventLoop loop;
connect(rp, SIGNAL(finished()), &loop, SLOT(quit())); //readready SIGNAL also tried
loop.exec();
QByteArray data = rp->readAll();
QString dataReply(data);
qDebug() << dataReply;

发生的情况是我总是收到 null。我已经尝试了几种组合,例如不同的信号(readyread、finished...)、使用 QUrlQuery 和 ByteArray、设置不同的 header ,但没有成功。

我还注意到,当我的参数在我的请求 ( https://address?parameters=string ) 中时,我可以收到返回值。 (在这种情况下,通过从“postdate”中删除参数并将它们包含在“req”中

QNetworkReply *rep = man->post(req,postdata);

RESTEStTEST 打印:我尝试使用 https://resttesttest.com/ 发布它工作正常。它给出了以下评论

有人可以帮我吗?

最佳答案

借助以下使用 curl 命令的帮助,我能够分析请求:

curl  --trace-ascii  output.txt \
-d "idm=0123" \
-L "https://script.google.com/macros/s/AKfycbzhHBpZ8jMfa2bwE_A0lQJNqOzVl894dcjkch_-0mNC6EX9usn1/exec"

在 .txt 文件中有以下行:

# ...
== Info: Issue another request to this URL: 'https://script.googleusercontent.com/macros/echo?user_content_key=odFvp9PfkjN-H6-IhBUWhj1XC4LbgGXq3moew78NtUkeQAI4UCwjEaz33jH1p_VSrMLvl9R-1p2f-LCuIURpcA9ynEuB76Kdm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnDmqvNuuY9_pIuNLYjrNDt9AJiVy2Q9_jt9ucsQKYIYmzKj2_WuA-BZmfTxlnQZT_cbqvF_hZXXo&lib=MbJJIMSCHwBgrN--y_1G9PbYl-ov70HvX'
== Info: Switch from POST to GET
# ...

这告诉我们,重定向从 POST 更改为 GET,因此不考虑属性 QNetworkRequest::NoLessSafeRedirectPolicy继续尝试使用 POST。所以解决方案是手动处理重定向。

QNetworkAccessManager manager;

QNetworkRequest request;
QUrl url("https://script.google.com/macros/s/AKfycbzhHBpZ8jMfa2bwE_A0lQJNqOzVl894dcjkch_-0mNC6EX9usn1/exec");
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrlQuery postData;
postData.addQueryItem("idm", "string");
QNetworkReply *reply_post = manager.post(request, postData.toString(QUrl::FullyEncoded).toUtf8());
QEventLoop loop;
QObject::connect(reply_post, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

QNetworkRequest redirect_request;
redirect_request.setUrl(reply_post->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl());
reply_post->deleteLater();
QNetworkReply *reply_get = manager.get(redirect_request);
QObject::connect(reply_get, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
qDebug()<< reply_get->readAll();
reply_get->deleteLater();

输出:

OK

关于c++ - QNetworkRequest 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582106/

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