Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Giuseppe is right, you don't need to use libcurl, curlpp and similar libraries. There is no need for that, Qt has a simple and working class on it own.

Keep in mind that the standard way of sending request and retrieving reply is asynchronous. You always have to connect the manager finished(QNetworkReply*) signal to a slot.

If you send multiple requests and don't want to add a slot for each reply, you can always run an event loop, and connect the managers signal to event loops quit() slot.

Something like this:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QEventLoop *eventLoop = new QEventLoop();
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), eventLoop, SLOT(quit());

manager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));
eventLoop->exec(QEventLoop::ExcludeUserInputEvents);

QByteArray replyData = reply->readAll();
... //do what you want with the data your receive from reply

Btw. don't know what are you doing. But if its a mobile app, I would recommend you switch from VS to QtCreator IDE. It has a nice simulator and a complete toolchain for mobile device testing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top