QNetworkRequest返回null

问题描述 投票:2回答:1

我必须使用帖子到谷歌脚本(见下文),我想在脚本的最后收到一个反馈,它一切顺利。

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;

会发生什么是我总是收到空。我已经尝试了几种组合,例如不同的信号(readyread,finished ...),使用QUrlQuery和ByteArray,设置不同的标题,但没有成功。

我也注意到,当我的参数在我的请求(https://address?parameters=string)中时,我可以收到回报。 (在这种情况下,从“postdate”中删除参数并将其包含在“req”后面

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

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

有人可以帮帮我吗?

c++ qt post qnetworkaccessmanager qnetworkreply
1个回答
0
投票

在使用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,因此不会考虑继续尝试使用POST的属性QNetworkRequest::NoLessSafeRedirectPolicy。所以解决方案是手动处理重定向。

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
© www.soinside.com 2019 - 2024. All rights reserved.