Simple QHTTP Example

Reading time : Less than 10 minutes Qt provides a very good class named Qhttp for doing http requests and to get response from the web. This class is thread safe and easy to use. To begin with, create a Qt console application and name it DoHttp, and add a reference about network usage in your project file.

Now add a new class to the project to handle the Qhttp object. You have to inherit from QObject to implement signals and slots. So set QObject as base class.

Add a Qhttp object to your new class and declare a couple of slots to handle signals emitted by it. Take a look at the header file.

#ifndef HANDLER_H
#define HANDLER_H

#include <QObject>
#include <QHttp>
#include <QDebug>

class Handler : public QObject
{
    Q_OBJECT
public:
    explicit Handler(QObject *parent = 0);
    void doHttp();

signals:

public slots:
    void stateChanged ( int state );
    void responseHeaderReceived ( const QHttpResponseHeader & resp );
    void requestFinished ( int id, bool error );
private:
    QHttp *http;
};

#endif // HANDLER_H

Finally, expand all the methods. The responseHeaderRecieved() signal is emitted by the Qhttp object when it receives the header for the response. Headers contains a couple of data including this content type and size. Once the request is finished, the requestFinished() signal is emitted. Don’t get confused with signals and slots. Our class implements all of the slots by the same name as the corresponding signal emitted by the Qhttp object. Take a look the source code.

#include "handler.h"

Handler::Handler(QObject *parent) :QObject(parent)  {
    http = new QHttp(this);
    connect(http, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
    connect(http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)), this, SLOT(responseHeaderReceived(QHttpResponseHeader)));
    connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));
}

void Handler::doHttp()  {
    http->setHost("google.com");
    http->get("/");
}

void Handler::stateChanged(int state)   {
    switch(state)   {
    case 0:
        qDebug() << "Unconnected";
        break;
    case 1:
        qDebug() << "Host Lookup";
        break;
    case 2:
        qDebug() << "Connecting";
        break;
    case 3:
        qDebug() << "Sending";
        break;
    case 4:
        qDebug() << "Reading";
        break;
    case 5:
        qDebug() << "Connect";
        break;
    case 6:
        qDebug() << "Closing";
        break;
    }
}

void Handler::responseHeaderReceived(const QHttpResponseHeader &resp)   {
    qDebug() << "Size : " << resp.contentLength();
    qDebug() << "Type : " << resp.contentType();
    qDebug() << "Status Code : " << resp.statusCode();
}

void Handler::requestFinished(int id, bool error)   {
    qDebug() << "Request Id : " << id;
    if(error)   {
        qDebug() << "Error";
    }   else    {
        qDebug() << http->readAll();
    }
}

Finally, include the class, make an object and run it.

#include <QtCore/QCoreApplication>
#include "handler.h"

int main(int argc, char *argv[])    {
    QCoreApplication a(argc, argv);
    Handler _handler;
    _handler.doHttp();
    return a.exec();
}

Take a look at the output.