QHttp在Qt5中不可用(QHttp in not available in Qt5)
我注意到Qt5中的QHttp类不再可用,我不断收到一条错误消息,说我需要使用QNetworkAccessManager来执行此操作。
有没有办法在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?
最满意答案
这是一个简单的HTTP帖子(我正在使用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; }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; }更多推荐
发布评论