简体   繁体   English

QNetworkRequest返回null

[英]QNetworkRequest returning null

I have to use post to a google script (see below) and I want in the end of the script receive a feedback from it that everything went okay. 我必须使用发布到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);
}

In my cpp code I have: 在我的cpp代码中,我有:

header (.h) 标头(.h)

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

Constructor: 构造函数:

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"));

Inside a function: 在函数内部:

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;

What happens is that I always receive null. 发生的事情是我总是收到空值。 I already tried several combinations such as different signals (readyread, finished...), using QUrlQuery and ByteArray, setting different headers, but with no success. 我已经使用QUrlQuery和ByteArray尝试了几种组合,例如不同的信号(readyread,finished ...),设置了不同的标头,但没有成功。

I also noticed that, when my parameters are in my request ( https://address?parameters=string ) I can receive the return. 我还注意到,当我的参数在请求中时( https:// address?parameters = string ),我可以收到返回值。 (In this case by removing the parameters from "postdate" and including them in post "req" (在这种情况下,请从“ postdate”中删除参数,并将其包含在post“ req”中

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

PRINT OF THE RESTEStTEST: I tried to post using https://resttesttest.com/ and it worked properly. RESTESTTEST的打印:我尝试使用https://resttesttest.com/进行发布,并且工作正常。 It gave the following comments 它给出了以下评论

Could someone help me please? 有人可以帮我吗?

With the help of the following command using curl I was able to analyze the request: 借助以下使用curl的命令,我能够分析请求:

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

In the .txt file is the following line: .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
# ...

That tells us that in the redirection is changed from POST to GET so it is not taken into account with the attribute QNetworkRequest::NoLessSafeRedirectPolicy that continues trying with POST. 这告诉我们,重定向已从POST更改为GET,因此继续尝试POST的属性QNetworkRequest :: NoLessSafeRedirectPolicy没有考虑到它。 So the solution is to handle the redirect manually. 因此解决方案是手动处理重定向。

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();

Output: 输出:

OK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM