我希望对网页做一个非常简单的POST请求。页面是在php中,将采取任何发布的检查对数据库,然后如果项目在数据库中,则使用密钥进行响应。
我不知道如何在Qt中使用post请求或如何获取返回的信息并将其存储回Qt中的变量。任何帮助都会受到高度赞赏,因为我从Qt方面的空白开始。
我看过其他例子:
https://stackoverflow.com/questions/11348359/qt-https-post-request
How can I POST data to a url using QNetworkAccessManager
但我没有看到如何存储来自php脚本的响应
答案 0 :(得分:4)
将完成的 QNetworkAccessManager 信号连接到您的广告位并使用 QNetworkReply ,您应该阅读该网页的所有内容。
这是一个例子,它可以很容易地适应post方法。
void MainWindow::on_pushButton_clicked()
{
QNetworkAccessManager * mgr = new QNetworkAccessManager(this);
connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(onfinish(QNetworkReply*)));
connect(mgr,SIGNAL(finished(QNetworkReply*)),mgr,SLOT(deleteLater()));
mgr->get(QNetworkRequest(QUrl("http://www.google.com")));
}
void MainWindow::onfinish(QNetworkReply *rep)
{
QByteArray bts = rep->readAll();
QString str(bts);
QMessageBox::information(this,"sal",str,"ok");
}
答案 1 :(得分:4)
opc0de以前的答案对我来说不是POST,而是GET。
这是如何执行POST请求
void xxx::postRequest(QByteArray & postData)
{
QUrl url = QUrl("abc.com");
QNetworkAccessManager * mgr = new QNetworkAccessManager(this);
connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinish(QNetworkReply*)));
connect(mgr,SIGNAL(finished(QNetworkReply*)),mgr,SLOT(deleteLater()));
QHttpMultiPart http;
QHttpPart receiptPart;
receiptPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
receiptPart.setBody(postData);
http.append(receiptPart);
mgr->post(QNetworkRequest(url), http);
}
void xxx::onFinish(QNetworkReply *rep)
{
}
来自文档here.
答案 2 :(得分:1)
QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QUrl url("https://accounts.google.com/o/oauth2/token");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrlQuery params;
params.addQueryItem("client_id", "...");
params.addQueryItem("client_secret", "...");
params.addQueryItem("code", "...");
// etc
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
manager->post(request, params.query().toUtf8());