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. How to use QNetworkAccessManager post
QtWS25 Last Chance

How to use QNetworkAccessManager post

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 7.7k Views
  • 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
    SamFaye
    wrote on last edited by
    #1

    I want to access a site via its URL to https using the post method.
    I did this
    @
    QUrl url (valUrl);
    QByteArray parameter;
    QString identifiant1 = "& id1 = valeur_id1";
    QString identifiant2 = "& id2 = valeur_id2";
    QString identifiant3 = "id2 = valeur_id3";
    parametre.append (identifiant1);
    parametre.append (identifiant2);
    parametre.append (identifiant3);

    maclasse.startRequest (url)

    @

    and in my class I have this reply

    @
    qnam.post = (QNetworkRequest (url), parameter);
    connect (reply, SIGNAL (readyRead ()), this, SLOT (httpReadyRead ()));
    connect (reply, SIGNAL (finished ()), this, SLOT (connectRequestFinished ()));
    connect (reply, SIGNAL (error (QNetworkReply: NetworkError)),
    this, SLOT (networkErrors (QNetworkReply: NetworkError)));
    @
    But I have this error message when running and displaying reply-> errorString (): "Erreur inconnue"
    What should I do?

    Edit: I wrapped your code in @ tags to make it readable. Please do that yourself next time; Andre

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Please, use '@' to format your code nicely, it's not really readable now.

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        You should avoid using the QUrl constructor with an argument. It does not work properly. Instead, use one of the static methods to construct a URL. This is a design error that will be corrected in Qt 5.

        This could be the cause of your problem already: an invalid URL.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SamFaye
          wrote on last edited by
          #4

          I do this @QUrl url; url.setUrl(valUrl);@

          An in my methode httpReadyRead I do this @if(!reply->readAll().isEmpty())
          {
          QFile compressedFile = new QFile("tempSAF");
          compressedFile->open(QIODevice::WriteOnly);
          compressedFile->write(reply->readAll());
          // char
          reponse = reply->readAll();
          qDebug() << "################# reply->readAll().size() = " << reply->readAll().size();
          // for(int i =0; i< sizeof(reponse);i++)
          // {
          // qDebug() << "################# reponse["<<i<<"] = " << reponse[i];
          // }
          //for(int)
          Etat = true;
          }
          else
          {
          qDebug() << "########### Impossible de réccupérer les données ";
          Etat=false;
          }@
          But this methode wase not executed. The reply is empty but the messe @qDebug() << "########### Impossible de réccupérer les données ";@ was not displayed.

          I have the same error

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SamFaye
            wrote on last edited by
            #5

            When I start debugging the value of url is: @synchronous data unavailable@ after the execution of this statement @reply =qnam.post(QNetworkRequest (url) m_pData);@

            m_pData has good values

            -NB-: I'm using https

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              So, you meant that the other qDebug gets displayed?

              I'm not exactly sure, but I seem to vaguely remember, that after you do readAll(), QNR is flushed, so you can't really use it again. But I can't find any info on that in Qt 4.7 documentation, so my memory might be wrong here. Try storing the reply and doing the 'if' on the stored data.

              (Z(:^

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [quote author="SamFaye" date="1314711148"]I do this @QUrl url; url.setUrl(valUrl);@
                [/quote]

                That method has exactly the same problem as the constructor. Please use one of the static methods like QUrl::fromEncoded().

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SamFaye
                  wrote on last edited by
                  #8

                  I have this error message when i use https and post @SSL: "Le nom d'hôte ne correspondait à aucun des hôtes valides pour ce certificat, Le certificat n'est pas sécurisé car auto-signé" @

                  After this message my application crache

                  My URL is like this: @https://Ip_machine/xxxxxxx@

                  What is missing??

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    You will have to handle SSL errors. See for example "this thread":http://developer.qt.nokia.com/forums/viewthread/5861/#35121.

                    bq. Note that calling ["ignoreSslErrors()":http://doc.qt.nokia.com/latest/qnetworkreply.html#ignoreSslErrors] without restraint may pose a security risk for your application. Use it with care.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SamFaye
                      wrote on last edited by
                      #10

                      I had done what Lukas Geyer said, now I didn't have the message but the application belong crashing

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on last edited by
                        #11

                        Where does it crash? Start the application in debug mode. The debugger will halt exactly where the application crashes.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SamFaye
                          wrote on last edited by
                          #12

                          This is because the other instructions are executed while the server response is not received. How to wait for the server response to continue execution of other instructions?

                          1 Reply Last reply
                          0
                          • sierdzioS Offline
                            sierdzioS Offline
                            sierdzio
                            Moderators
                            wrote on last edited by
                            #13

                            For example, you can use:
                            @
                            forever {
                            if (replyReceived) {
                            processReply();
                            break;
                            }
                            else
                            qApp->processEvents();
                            }
                            @
                            This will not block your app. Substitute if condition and processing with your own.

                            (Z(:^

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lgeyer
                              wrote on last edited by
                              #14

                              [quote author="SamFaye" date="1314899811"]This is because the other instructions are executed while the server response is not received. How to wait for the server response to continue execution of other instructions?[/quote]

                              Which other instructions? QNAM emits the finished signal when the response is available. This is where your response handling code has to go.

                              Blocking your application until data is available is usually not the solution, it just covers a severe design flaw.

                              1 Reply Last reply
                              0

                              • Login

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