QNetworkRequest 返回 null
QNetworkRequest returning null
我必须将 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 代码中我有:
header (.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,设置不同的 headers,但没有成功。
我还注意到,当我的参数在我的请求中时 (https://address?parameters=string),我可以收到 return。 (在这种情况下,通过从 "postdate" 中删除参数并将它们包含在 post "req"
QNetworkReply *rep = man->post(req,postdata);
RESTEStTEST 打印:我尝试使用 https://resttesttest.com/ post 并且它工作正常。它给出了以下评论
有人可以帮我吗?
借助以下使用 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
我必须将 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 代码中我有:
header (.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,设置不同的 headers,但没有成功。
我还注意到,当我的参数在我的请求中时 (https://address?parameters=string),我可以收到 return。 (在这种情况下,通过从 "postdate" 中删除参数并将它们包含在 post "req"
QNetworkReply *rep = man->post(req,postdata);
RESTEStTEST 打印:我尝试使用 https://resttesttest.com/ post 并且它工作正常。它给出了以下评论
有人可以帮我吗?
借助以下使用 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