需求
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请求内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论