Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Failed at rewriting a simple HTTP request with QNetworkAccessManager (formely written with curl)

    General and Desktop
    4
    10
    5488
    Loading More Posts
    • 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.
    • F
      floatingWoods last edited by

      Hello,
      I have a Windows application that uses Curl (or LibCurl) to request a simple data from a server with HTTP. I rewrote it with QNetworkAccessManager, but the result is not the same (the server receives the data, but doesn't recognize it). I am wondering what I could have done wrong. Here is the original (Curl) version:
      @
      curl_global_init(CURL_GLOBAL_ALL);
      CURL curl;
      CURLcode res;
      curl=curl_easy_init();
      if (curl)
      {
      curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curlWriteFunction); // callback to read the reply
      curl_easy_setopt(curl,CURLOPT_URL,"http://test.dynalias.net?data1=someText1&data2=someText2");
      res=curl_easy_perform(curl);
      if (res==CURLE_OK)
      ...get the data the callback read
      @
      and here is what I wrote in Qt:
      @
      QNetworkAccessManager _access;
      connect(&_access,SIGNAL(finished(QNetworkReply
      )),this,SLOT(_httpReplyFinished(QNetworkReply*))); // _httpReplyFinished will read returned data
      QNetworkRequest request(QUrl("http://test.dynalias.net?data1=someText1&data2=someText2",QUrl::StrictMode));
      _access.get(request);
      QEventLoop el;
      connect(&_access,SIGNAL(finished(QNetworkReply*)),&el,SLOT(quit())); // make sure the event loop ends when the job finishes!
      el.exec();
      ...if we arrived here, the request was sent and a reply read
      @

      The main problem is that the server doesn't understand the data when sent with the Qt code (but I can confirm that something arrives at the server side). Oh and obviously the "test" in "test.dynalias.net" is a different word. There must be an obvious and simple error I made, but I can't figure out. Any help is appreciated.

      1 Reply Last reply Reply Quote 0
      • D
        dialingo last edited by

        So you are getting a different feedback from the server with libCurl and Qt? A thorough but cumbersome way to find the reason is to use a network monitor like Wireshark. With this tool you can make your requests look identic on the wire and therefore you will get identic results.
        Does your server discriminate your solutions because you set different user agent strings? Does your server request a redirect and you have configured only one solution to follow?

        1 Reply Last reply Reply Quote 0
        • F
          floatingWoods last edited by

          Thanks a lot for your reply dialingo,

          Actually there are two problems in my case:

          1. I don't have the source code of the server application. So I cannot analyze what is wrong on that side, nor can I rewrite the socket comunication on that side.

          2. I have very little knowledge related to sockets and protocols.

          But I know that the server does just a simple thing, and the request string that I sent from libCurl or Qt is exactly the same. So maybe Qt somehow modifies my string before sending it? (e.g. autocorrect or similar?)

          1 Reply Last reply Reply Quote 0
          • D
            dangelog last edited by

            The best thing is dumping the traffing using a network sniffer and trying to figure out the differences.

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply Reply Quote 0
            • F
              floatingWoods last edited by

              Thank you Peppe,

              Yes, I will do that as soon as possible. I'll then try with "Wireshark".

              1 Reply Last reply Reply Quote 0
              • F
                floatingWoods last edited by

                Hi again, I used "Wireshark" to have a look at my data that I am sending out. Following is the difference that I could notice:

                When using Curl:

                @
                Request Method: GET
                Request URI [truncated]: /?data1=aaa...
                Request Version: HTTP/1.1
                Host: xxx.dynalias.net\r\n
                Accept: /\r\n
                \r\n
                @

                When using QNetworkAccessManager:

                @
                Request Method: GET
                Request URI [truncated]: /?data1=aaa...
                Request Version: HTTP/1.1
                Connection: Keep-Alive\r\n
                Accept-Encoding: gzip\r\n
                Accept-Language: de-CH,en,*\r\n
                user-Agent: Mozilla/5.0\r\n
                Host: xxx.dynalias.net\r\n
                \r\n
                @

                So it seems that with the QNetworkAccessManager I send some more information (Accept, Connection, Accept-Encoding, Accept-Language and User-Agent). To me, that doesn't ring a bell at all.

                Does anyone have a clue?

                [EDIT: code formatting for wire data, Volker]

                1 Reply Last reply Reply Quote 0
                • D
                  dangelog last edited by

                  I suspect something wrong on server side. Try using a command line tool like lwp-request to build your GET requests and add headers till it breaks.

                  Software Engineer
                  KDAB (UK) Ltd., a KDAB Group company

                  1 Reply Last reply Reply Quote 0
                  • D
                    dialingo last edited by

                    Hey Peppe, that's a good idea!
                    floatingWoods, did you check already whether your 2 solutions achieve the same result when you fetch a no-frills page?

                    1 Reply Last reply Reply Quote 0
                    • F
                      floatingWoods last edited by

                      Thanks Peppe and Dialingo for your support.
                      Actually I realized with your help that the server side had quite many problems. Since I don't have the source code, I will have to reprogram it, maybe by using simple tcp socket communication for high robustness.

                      Cheers for your help!

                      1 Reply Last reply Reply Quote 0
                      • P
                        pavlonion last edited by

                        You could use "QtCUrl":https://github.com/pavlonion/qtcurl/blob/master/README.md for convenient usage of cUrl in Qt projects

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post