Search code examples
c++qtqt5qnetworkaccessmanagerqhttp

QHttp in not available in Qt5


I noticed that the QHttp class is no longer available in Qt5 and I keep getting an error message which says that I need to use the QNetworkAccessManager to do this.

Is there a way to access this class in Qt5?


Solution

  • This is a simple HTTP post (I am using Qt 5.3.2)

    int Connection::postRequest(QString requestType, QUrl params){
        QString params_array = params.query();
    
        QNetworkRequest request(user->url);
        request.setHeader(QNetworkRequest::ContentLengthHeader, QByteArray::number(params_array.size()));
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    
        QEventLoop waitLoop;
        QNetworkAccessManager* connection = new QNetworkAccessManager(/*`this` can also be passed*/);
        QNetworkReply* reply = connection->post(request, params_array.toUtf8());
        QObject::connect(reply, SIGNAL(finished()), &waitLoop, SLOT(quit()));
        waitLoop.exec();
    
        int errorCode = reply->error();
        if (errorCode != 0){
            // Show Error Message
        }
        else{
            // Parse "reply"
        }
    
        delete reply;
        delete connection;
        return errorCode;
    }