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. About HTTP requests and PHP variables
Qt 6.11 is out! See what's new in the release blog

About HTTP requests and PHP variables

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 8.5k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #1

    I'm using a QNetworkAccessManager item to get a text file from an http server containing a list of ip addresses, in my main program I try to connect to those addresses and if it doesn't succeed the ip address is removed from the text file and this is the hard part. I developed a php script to delete the unresponsive ip from the list but it requires the ip to be passed as an argument through the url (the php GET method), the script works fine when accessed from a browser but when I try accessing it through my program using
    @
    // Examples definitions:
    // QNetworkAccessManager http;
    // QString Giocatori::PathToHttpServer="http://myhost.com";
    // QString ipnr="127.0.0.1";

    QUrl url(Giocatori::PathToHttpServer+"/NotResponding.php");
    url.addQueryItem("IPNotResponding",ipnr);
    http.get(QNetworkRequest(url));
    @

    it doesn't work at all. I already tried to feed him the complete url string but it doesn't change behavior.
    I searched this and other forums and looked at the examples but I didn't find anything that helped, please save me!
    Thanks in advance

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Is your URL working on the command line? You can try with tools like curl or wget.

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

        Does the QNetworkAccessManager live long enough to actually execute the request ? You seem to allocate it on the stack, and it might be destroyed before the control is given back to the event loop.

        And are you even running an event loop (QCoreApplication::, QThread::, QEventLoop:: or QApplication::exec() ) ?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @Volker: I tried with curl and it works fine, this is worrying :(

          @alexisdm: The definitions I gave there were just examples to let you understand what type were the variables, they are not the true definitions, the manager is allocated on the heap, it's part of a widget and I started the event queue and the other functions that use it work perfectly, the problem arises just when I try to pass variables through the url

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alexisdm
            wrote on last edited by
            #5

            Does your php script or web server make a HTTP redirection ?

            QNetworkAccessManager doesn't handle redirections, you have to check the http status code from the QNetworkReply ("attribute()":http://qt-project.org/doc/qt-4.8/qnetworkreply.html#attributex with "QNetworkRequest::HttpStatusCodeAttribute":http://qt-project.org/doc/qt-4.8/qnetworkrequest.html#Attribute-enum) and relaunch another request with the new URL (the browsers, curl and wget do that automatically).

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Now my network manager handles redirections:
              @GestoreServers::GestoreServers(QObject parent)
              : QObject(parent)
              {
              http=new QNetworkAccessManager(this);
              connect(http, SIGNAL(finished(QNetworkReply
              )), this, SLOT(httpDone(QNetworkReply*)));
              }@
              @void GestoreServers::httpDone(QNetworkReply* reply)
              {
              QString fetched("");
              if (reply->error() != QNetworkReply::NoError) {
              emit erroreConnessione();
              errore=true;
              }
              else{
              QUrl redirectUrl=reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
              if (!redirectUrl.isEmpty() && redirectUrl!=OldUrl){
              OldUrl=redirectUrl;
              getFile(OldUrl);
              }
              else{
              QTextStream in(reply);
              while(!in.atEnd()) {
              fetched.append(in.readLine());
              fetched.append('\n');
              }
              }
              }
              emit done(fetched);

              }@
              but still no luck :(

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                Forgot to mention that the redirection is never executed, I put a debug breakpoint in that function to check if the redirection really happened and neither my host nor my script (about the script I was sure a priori) ever redirect the url. I'm beginning to become desperate :(

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alexisdm
                  wrote on last edited by
                  #8

                  You could use a network sniffer (wireshark) to see if there is any difference between what Qt and curl are sending to the server.

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    The sniffer was helpfull. it seems like my request isn't sent at all. so it never emits the finished signal :(

                    What did I do wrong?

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      i solved one problem but another arises. I managed to successfully get to the script with the correct variable, I did it just by deleting the QNAM each time if finishes processing a request and recreate it when a new request is sent:
                      @void GestoreServers::getFile(const QUrl &url)
                      {
                      QNetworkRequest indirizzo(url);
                      http=new QNetworkAccessManager(this);
                      connect(http, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpDone(QNetworkReply*)));
                      http->get(indirizzo);
                      }
                      void GestoreServers::httpDone(QNetworkReply* reply)
                      {
                      QString fetched("");
                      if (reply->error() != QNetworkReply::NoError) {
                      emit erroreConnessione();
                      }
                      else{
                      QUrl redirectUrl=reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
                      if (!redirectUrl.isEmpty() && redirectUrl!=OldUrl){
                      OldUrl=redirectUrl;
                      getFile(OldUrl);
                      }
                      else{
                      QTextStream in(reply);
                      while(!in.atEnd()) {
                      fetched.append(in.readLine());
                      fetched.append('\n');
                      }
                      }
                      }
                      emit done(fetched);
                      http->deleteLater();

                      }@
                      now the problem is that the requests without variables passed emits the finished() signal, but when I access the script passing the variable the finished signal is never emitted even if the script actually worked :(

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        QNAM is not designed to be deleted and re-created frequently. You just need to keep track of the requests and replies and connect the signals to some slots that handle the data.

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          [quote author="Volker" date="1335806200"]QNAM is not designed to be deleted and re-created frequently. You just need to keep track of the requests and replies and connect the signals to some slots that handle the data.[/quote]

                          That's what i did before, but after I send a get once and it emits a finished() signal using get on the same object with another url doesn't seems to work, as I said before the request wasn't sent at all... any idea why that happened?

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            You need to create a new QNetworkRequest and use that on the existing QNAM. Make sure to connect to the signals of the newly returned QNetworkReply (or to those of the QNAM).

                            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