Failed at rewriting a simple HTTP request with QNetworkAccessManager (formely written with curl)
-
wrote on 20 Oct 2011, 19:53 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.
-
wrote on 15 Nov 2011, 08:46 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? -
wrote on 15 Nov 2011, 14:54 last edited by
Thanks a lot for your reply dialingo,
Actually there are two problems in my case:
-
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.
-
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?)
-
-
wrote on 15 Nov 2011, 15:00 last edited by
The best thing is dumping the traffing using a network sniffer and trying to figure out the differences.
-
wrote on 15 Nov 2011, 15:04 last edited by
Thank you Peppe,
Yes, I will do that as soon as possible. I'll then try with "Wireshark".
-
wrote on 18 Nov 2011, 13:30 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]
-
wrote on 18 Nov 2011, 13:42 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.
-
wrote on 18 Nov 2011, 16:25 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? -
wrote on 20 Nov 2011, 12:42 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!
-
wrote on 16 Jun 2013, 16:53 last edited by
You could use "QtCUrl":https://github.com/pavlonion/qtcurl/blob/master/README.md for convenient usage of cUrl in Qt projects