当前位置: 代码网 > it编程>编程语言>C/C++ > 使用Qt封装一个发送http请求通用类

使用Qt封装一个发送http请求通用类

2024年12月03日 C/C++ 我要评论
需求qt 代码中使用qnetworkrequest、qnetworkreply进行http请求,能不能整理成一个通用的方法或者类。实现httpclient类#include <qobject&g

需求

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com