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?
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?
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;
}
Use QNetworkAccessManager
in Qt 5. You can use an event loop to wait until the reply is finished and then read the available bytes :
QString My_class::My_Method()
{
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(myURL)));
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
loop.exec();
QByteArray bts = reply->readAll();
QString str(bts);
delete reply;
return str;
}
You can also do it in an asynchronous way by connecting the finished
signal of the QNetworkAccessManager
to a slot :
connect(&manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinished(QNetworkReply*)));
And read data there :
void onFinished(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bts = reply->readAll();
...
}
}
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;
}
Assuming that you don't need to maintain a huge code base, the right way is to use new QNetworkAccessManager class instead.
© 2022 - 2024 — McMap. All rights reserved.