QHttp在Qt5中不可用(QHttp in not available in Qt5)

编程入门 行业动态 更新时间:2024-10-27 23:19:03
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; }

更多推荐

本文发布于:2023-07-28 22:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   QHttp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!