使用Qt封装一个发送http请求通用类
作者:Lydro
这篇文章主要为大家详细介绍了如何使用Qt封装一个通用类,可以通过QNetworkRequest和QNetworkReply进行http请求,感兴趣的可以了解一下
需求
Qt 代码中使用QNetworkRequest、QNetworkReply进行http请求,能不能整理成一个通用的方法或者类。
实现
HttpClient类
#include <QObject> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <QUrl> #include <QUrlQuery> #include <QByteArray> #include <QJsonObject> #include <QJsonDocument> #include <QDebug> #include <QEventLoop> class HttpClient : public QObject { Q_OBJECT public: explicit HttpClient(QObject *parent = nullptr) : QObject(parent), networkManager(new QNetworkAccessManager(this)) { connect(networkManager, &QNetworkAccessManager::finished, this, &HttpClient::onReplyFinished); } void get(const QUrl &url, const QUrlQuery &query = QUrlQuery()) { QUrl fullUrl = url; fullUrl.setQuery(query); QNetworkRequest request(fullUrl); networkManager->get(request); eventLoot->exec(); } void post(const QUrl &url, const QByteArray &data) { QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); networkManager->post(request, data); eventLoot->exec(); } signals: void requestFinished(const QByteArray &responseData, QNetworkReply::NetworkError error); private slots: void onReplyFinished(QNetworkReply *reply) { if (reply->error() == QNetworkReply::NoError) { QByteArray responseData = reply->readAll(); emit requestFinished(responseData, QNetworkReply::NoError); } else { emit requestFinished(QByteArray(), reply->error()); } reply->deleteLater(); } private: QNetworkAccessManager *networkManager; QEventLoop eventLoop; };
HttpClient中使用了一个QEventLoop,为了使发送的事件得到及时的处理,不加这个的话,测试发现半天都不能调到onReplyFinished槽函数中。
使用
#include <QCoreApplication> #include <QUrl> #include <QUrlQuery> #include <QJsonDocument> #include <QJsonObject> #include <QByteArray> #include "HttpClient.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); HttpClient client; // 发送GET请求 QUrl getUrl("https://jsonplaceholder.typicode.com/posts/1"); QObject::connect(&client, &HttpClient::requestFinished, [](const QByteArray &responseData, QNetworkReply::NetworkError error) { if (error == QNetworkReply::NoError) { QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData); QJsonObject jsonObj = jsonDoc.object(); qDebug() << "GET Response:" << jsonObj; } else { qDebug() << "GET Error:" << error; } }); client.get(getUrl); // 发送POST请求 QUrl postUrl("https://jsonplaceholder.typicode.com/posts"); QJsonObject postData; postData["title"] = "foo"; postData["body"] = "bar"; postData["userId"] = 1; QByteArray postDataBytes = QJsonDocument(postData).toJson(); QObject::connect(&client, &HttpClient::requestFinished, [](const QByteArray &responseData, QNetworkReply::NetworkError error) { if (error == QNetworkReply::NoError) { QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData); QJsonObject jsonObj = jsonDoc.object(); qDebug() << "POST Response:" << jsonObj; } else { qDebug() << "POST Error:" << error; } }); client.post(postUrl, postDataBytes); return a.exec(); }
到此这篇关于使用Qt封装一个发送http请求通用类的文章就介绍到这了,更多相关Qt发送http请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!