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



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



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



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



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



  • Thank you Peppe,

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



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



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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.