Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making HTTP Requests in Qt

I'm new to Qt. I installed Qt for VS2008 and integrated with my VS2010. I just want to know how to make HTTP requests. I've read about QtNetwork but QtHttp is obselete.

I also know about libcurl and curlpp, but I have problems installing it, and making it work with Qt.

What do you recommend, QtNetwork or curlpp? If QtNetwork, can you please give me a sample function or piece of code (and what class to use). If curlpp(libcurl), can you please point me to somewhere where I can find the steps to install it for Qt (or kindly explain)?

Thank you very much.

like image 262
Ruel Avatar asked Dec 21 '22 21:12

Ruel


1 Answers

libcurl and curlpp are great libraries, but using them adds a dependency to your project that probably you can avoid.

Recent versions of Qt recommend to use QNetworkAccessManager to make network requests (included http requests) and receive replies.

The simplest possible way to download a file is:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));

When the replyFinished slot is called, the QNetworkReply object that it takes as parameter will contain the downloaded data as well as meta-data (headers, etc.).

A more complete example can be found in the Qt examples, you can read here its source code.

like image 82
Giuseppe Cardone Avatar answered Dec 28 '22 12:12

Giuseppe Cardone