Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAccessManager doesn't send body
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager doesn't send body

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 520 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Subuday
    wrote on last edited by Subuday
    #1

    I can't figure out why sometimes QNetworkAccessManager doen't send body,
    it's a very strange. It happens if I quickly press button send request.

    void Client::usernameFindRequest(const QString &username)
    {
        QJsonObject userObject;
        userObject["username"] = username;
    
        QNetworkRequest request(url + "forgotPassword/username");
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        QNetworkReply *reply = manager->post(request, QJsonDocument(userObject).toJson(QJsonDocument::Compact));
        connect(reply, SIGNAL(finished()),this, SLOT(usernameFindReply()));
    }
    
    void Client::usernameFindReply()
    {
        QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
        qDebug() << "TeST";
    
        if (reply->error() == QNetworkReply::NoError)
        {
          qDebug() << "OK!";
          QByteArray httpReply = reply->readAll();
          qDebug() << httpReply;
          emit usernameFindStatus(true);
        }
        else
        {
            qDebug() << reply->errorString();
            emit usernameFindStatus(false);
        }
        manager->clearAccessCache();
        reply->deleteLater();
    }
    

    0_1558042102774_Screenshot from 2019-05-17 00-27-16.png

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you sure all the requests are getting processed ?

      QNetworkAccessManager processes up to 6 request in parallel. Others are queue.

      By the way, where are you sending the request at ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Are you sure all the requests are getting processed ?

        QNetworkAccessManager processes up to 6 request in parallel. Others are queue.

        By the way, where are you sending the request at ?

        S Offline
        S Offline
        Subuday
        wrote on last edited by
        #3

        @SGaist I send it on my http server which send me reply.

        void HttpController::run()
        {
          socket = new QTcpSocket();
        
          if (!socket->setSocketDescriptor(this->socketDescriptor) )
          {
            emit error (socket->error());
            return;
          }
        
          //connect(socket, SIGNAL(connected()), this, SLOT(disconnected()), Qt::DirectConnection);
          connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::DirectConnection);
          connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
        
          exec();
        }
        
        
        void HttpController::readyRead()
        {
          if(socket->bytesAvailable())
          {
              //handle and parse are my functions which procees http request and send reply
              handle(parse(socket->readAll()));
              socket->disconnectFromHost();
          }
          else
          {
              qDebug() << "could not receive data";
              socket->close();
          }
        }
        
        
        void HttpController::disconnected()
        {
          socket->deleteLater();
          exit(0);
        }
        
        void HttpController::badRequest() const
        {
            QString reply;
        
            QLocale locale(QLocale::English);
            QDateTime currTime = QDateTime::currentDateTimeUtc();
            QString time = "Date: " + locale.toString(currTime, "ddd, ") + locale.toString(currTime, "d MMM yyyy HH:mm:ss") + " GMT";
        
            reply = "HTTP/1.1 400 Bad Request\r\n%1\r\n";
        
            socket->write(reply.arg(time).toLocal8Bit());
            socket->flush();
            socket->waitForBytesWritten();
        }
        
        void HttpController::authenticationReply(const QString &username, const QString &password)
        {
            bool ok = storage->authentication(username, password);
        
            QString reply;
        
            QJsonObject userObject;
            QString body = "";
            int bodyLength = 0;
        
            QLocale locale(QLocale::English);
            QDateTime currTime = QDateTime::currentDateTimeUtc();
            QString time = "Date: " + locale.toString(currTime, "ddd, ") + locale.toString(currTime, "d MMM yyyy HH:mm:ss") + " GMT";
        
            if(ok)
            {
                reply = "HTTP/1.1 200 OK\r\n%1\r\nContent-type: application/json\r\nContent-length: %2\r\n\r\n%3";
                userObject["succes"] = "true";
                body = QJsonDocument(userObject).toJson(QJsonDocument::Compact);
            }
            else
            {
                reply = "HTTP/1.1 401 Unauthorized\r\n%1\r\nContent-type: application/json\r\nContent-length: %2\r\n\r\n%3";
                userObject["succes"] = "false";
                body = QJsonDocument(userObject).toJson(QJsonDocument::Compact);
            }
        
            socket->write(reply.arg(time).arg(bodyLength).arg(body).toLocal8Bit());
            socket->flush();
            socket->waitForBytesWritten();
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          From the looks of it, you are expecting that when readyRead is called, all data have arrived which is wrong. You have to first ensure that everything has arrived before parsing it.

          Just in case, there's now a QtHttpServer module that might be of interest for your use case.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved