Question

I have used this code to sent the user name and a password to the server using POST Method. This returns me a reply,So how can I access the reply variable in the way that i can read the data sent from the server back to me ??

Used Code:

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)
{
//????????????
}
Was it helpful?

Solution

QNetworkReply is a QIODevice, so you can read it the same way you would read a file. But you have to destroy the QNetworkReply and check for error too in that slot.

For example, in the simplest case (without HTTP redirection):

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
        ...
    }
}

You should probably declare the QNetworkAccessManager variable as a member of your class instead of creating a new one for each request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top