我已使用此代码使用POST方法将用户名和密码发送到服务器。 这给我一个回复,那么如何才能以我能读取服务器发送给我的数据的方式访问回复变量?
void MainWindow::post(QString name, QString password)
{
QUrl serviceUrl = QUrl("http://lascivio.co/mobile/post.php");
QByteArray postData;
QString s = "param1="+name+"&";
postData.append(s);
s = "param2=" +password;
postData.append(s);
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
QNetworkRequest request;
request.setUrl(serviceUrl);
QNetworkReply* reply = networkManager->post(request, postData);
}
void MainWindow::serviceRequestFinished(QNetworkReply* reply)
{
//????????????
}
答案 0 :(得分:3)
QNetworkReply
是QIODevice
,因此您可以像读取文件一样阅读它。但是你必须销毁QNetworkReply
并在那个插槽中检查错误。
例如,在最简单的情况下(没有HTTP重定向):
void MainWindow::serviceRequestFinished(QNetworkReply* reply)
{
// At the end of that slot, we won't need it anymore
reply->deleteLater();
if(reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
// do something with data
...
} else {
// Handle the error
...
}
}
您可能应该将QNetworkAccessManager
变量声明为您班级的成员,而不是为每个请求创建一个新变量。