QNetworkAccessManager returning empty results



  • Hi all,
    I am struggling with qnetworkaccessmanager for quite sometime. I googled a lot, but I donot find a solution for this.

    I am creating a client using qaccessmanager to talk with a rest server. QNetworkReply is not returning any results. The server is working properly but the client is not returning results. On top of that the server gets called 3 times and sometimes the server is crashing. Hope some one can figure out what is going wrong. I am attaching the client code. Also the ready read is not firing. The readyread is properly connected to the slot.

    I tried different approches like connecting finished signal of networkaccessmanager, qnetworkreply e.t.c. But all of them ends up in giving the same error "Connection Closed" or the readAll bytearray being empty.

    @void RestClientCore::ConnectToServer()
    {
    m_NetworkManager = new QNetworkAccessManager(this);

    QUrl url("http://localhost");
    url.setPort(5432);
    QByteArray postData;
     postData.append("/?userid=user");
     postData.append("&site=site");
    QNetworkReply *reply = m_NetworkManager->post(request,postData);
    connect(reply, SIGNAL(readyRead()),this, SLOT(slotReadyRead()));
    connect(reply, SIGNAL(finished()), this, SLOT(onRequestCompleted()));
    

    }

    void RestClientCore::onRequestCompleted() {

    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    if(reply->error())
    {
    qDebug() <<reply->bytesAvailable() << reply->errorString();
    }
    else
    {
    qDebug() << reply->readAll();
    }
    reply->deleteLater();
    }

    void RestClientCore::slotReadyRead()
    {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    qDebug() << reply->readAll();
    }@

    Thanks in advance
    Regards
    Rejo Raj



  • What is request in line 10:
    @
    QNetworkReply *reply = m_NetworkManager->post(request,postData);
    @

    I dont' think you need the readyRead signal, and you can check the isFinished() method on reply finished (in the onRequestCompleted.

    Connect the error signal, maybe it gives you more details on what is going on.



  • I am sorry. I forgot to add the details of the request. I am adding the code. The error signal is there in my actual implementation. It just says Connection closed. Nothing other than that.

    I used the reply->isFinished() as you said. Still the reply is emty there and the server is calling thrice. First time correctly and the second and third time with empty post requests

    @void RestClientCore::ConnectToServer()
    {
    m_NetworkManager = new QNetworkAccessManager(this);

    QUrl url("http://localhost");
    url.setPort(5432);
    

    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader,
    "application/x-www-form-urlencoded");

    QByteArray postData;
     postData.append("/?userid=user");
     postData.append("&site=site");
    QNetworkReply *reply = m_NetworkManager->post(request,postData);
    connect(reply, SIGNAL(readyRead()),this, SLOT(slotReadyRead()));
    connect(reply, SIGNAL(finished()), this, SLOT(onRequestCompleted()));
    

    }

    void RestClientCore::onRequestCompleted()
    {

    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    if(reply->error())
    {
    qDebug() <<reply->bytesAvailable() << reply->errorString();
    }
    if(reply->isFinished())
    {
    qDebug() << "Reply Finished" << reply->readAll();
    reply->deleteLater();
    }
    }

    void RestClientCore::slotReadyRead()
    {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    qDebug() << reply->readAll();
    }
    @


  • Lifetime Qt Champion

    Hi,

    @m_NetworkManager = new QNetworkAccessManager(this);@

    Should be in RestClientCore constructor. You are creating a new QNAM each time you call ConnectToServer and never delete the old one.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.