How to use QNetworkAccessManager post

  • 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

    @ = (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

  • Moderators

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

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

  • 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");
    // char
    reponse = reply->readAll();
    qDebug() << "################# reply->readAll().size() = " << reply->readAll().size();
    // for(int i =0; i< sizeof(reponse);i++)
    // {
    // qDebug() << "################# reponse["<<i<<"] = " << reponse[i];
    // }
    Etat = true;
    qDebug() << "########### Impossible de réccupérer les données ";
    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

  • When I start debugging the value of url is: @synchronous data unavailable@ after the execution of this statement @reply (url) m_pData);@

    m_pData has good values

    -NB-: I'm using https

  • Moderators

    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.

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

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

  • 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??

  • You will have to handle SSL errors. See for example "this thread":

    bq. Note that calling ["ignoreSslErrors()":] without restraint may pose a security risk for your application. Use it with care.

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

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

  • 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?

  • Moderators

    For example, you can use:
    forever {
    if (replyReceived) {
    This will not block your app. Substitute if condition and processing with your own.

  • [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.

Log in to reply