Qt封装的libcurl http请求类

Qt封装的libcurl http请求类

/*
    Copyright: YiChen

    Contact email: 317090996@qq.com
*/
#ifndef YCURL_H
#define YCURL_H

#include <QObject>
#include "YCHttp.h"
#include <list>
#include <string>
#include <functional>
#include <map>
#include <memory>
#include <queue>

#ifdef Q_OS_WIN
#include "D:/MP/github/vcpkg/installed/x64-windows/include/curl/curl.h"
#else
#ifndef Q_OS_ANDROID
#include "/usr/local/curl/include/curl/curl.h"
#endif
#endif

class YCUrl : public QObject
{
    Q_OBJECT
public:
    explicit YCUrl(QObject *parent = nullptr);
    ~YCUrl();

    static bool Init();
    static void Cleanup();
    QVariant m_statusCode;
    bool get(const QString &url,QString &responseData, YCHeaderIndex nIndex = URL2_None,int nTimeOut = 10*1000);
//    bool get(const QByteArray &url,QByteArray &responseByte, YCHeaderIndex nIndex = URL2_None,int nTimeOut = 10*1000);
    bool get(const QString &url,QByteArray &responseByte, YCHeaderIndex nIndex = URL2_None,int nTimeOut = 10*1000);
    bool getImage(const QString &url, QPixmap &responseData, YCHeaderIndex nIndex = URL2_None, QByteArray postData = "", int nTimeOut = 10*1000);
//    bool getImage(const QByteArray &url, QPixmap &responseData, YCHeaderIndex nIndex = URL2_None, QByteArray postData = "", int nTimeOut = 10*1000);
    bool post(const QString &url,QByteArray &postByte,QByteArray &responseByte, YCHeaderIndex nIndex = URL2_PostDefaut,int nTimeOut = 10*1000);
//    bool post(const QByteArray &url,QByteArray &postByte,QByteArray &responseByte, YCHeaderIndex nIndex = URL2_PostDefaut,int nTimeOut = 10*1000);
    bool post(const QString &url,const QUrlQuery &query,QString &responseData, YCHeaderIndex nIndex = URL2_PostDefaut,int nTimeOut = 10*1000);
    bool post(const QString &url,const QUrlQuery &query,QByteArray &responseByte, YCHeaderIndex nIndex = URL2_PostDefaut,int nTimeOut = 10*1000);

    void setCookie(const QString &name,const QString &value);
    void setTotalCookies(const QList<QNetworkCookie>& cookieList);
    QList<QNetworkCookie> getTotalCookies();
    void updateCK(const QNetworkCookie &netCookie);
    CURLcode httpRequest(const QByteArray& strUrl, int nHttpType = YCHTTP_POST_BYTE, const QByteArray& bytePost = QByteArray(), YCHeaderIndex nIndex = URL2_None,
                     int nTimeOut = 10*1000);
    static size_t ResponseHeaderFunction(char *buffer, size_t size, size_t nitems, void *userdata);
    static size_t ResponseContentFunction(char *ptr, size_t size, size_t nmemb, void *userdata);

    static bool						s_bInit;					//是否初始化
    static CURLSH*					s_pShareHandle;				//共享curl句柄
    QMutex m_mutex;
    CURL							*m_hCurl;				//easycurl句柄
    QStringList m_header;
    void setHeader(curl_slist **header, YCHeaderIndex nIndex)
    {//不要手动添加"Accept-Encoding","gzip, deflate, br",Qt会自动添加并解压
     //不要手动添加"Content-Length"  Qt会自动添加
     //自动添加Host:
     //默认自动添加Accept-Language:zh-CN,en,*
     //Connection:Keep-Alive
     //User-Agent:Mozilla/5.0
        switch(nIndex)
        {
        case URL2_None:
        {
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "");
            break;
        }
        case URL2_GetDefaut:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "User-Agent:Mozilla/5.0");
            break;
        case URL2_PostDefaut:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Content-Type:application/x-www-form-urlencoded; charset=UTF-8");
            break;
        case URL2_PostJson:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Content-Type:application/json");
            break;
        case URL2_PostXml:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Content-Type:application/xml");
            break;
        case URL2_PostZip:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Content-Type:application/zip");
            break;
        case URL2_PostPlainText:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Content-Type:text/plain");
            break;
        case URL2_PostHtml:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Content-Type:text/html");
            break;
        case URL2_PostPng:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Content-Type:image/png");
            break;
        case URL2_PostJpg:
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Content-Type:image/jpg");
            break;
        case URL2_VerifyCode:    //12306
        {
            //qDebug() << "12306pppp";
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Referer: https://kyfw.12306.cn/otn/login/init");
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Connection: keep-alive");
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Accept: application/json, text/javascript,*/*; q=0.8");
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Accept-Encoding: gzip, deflate, br");
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "Accept-Language: zh-CN,zh;q=0.8");
            *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36");
            break;
        }
        case URL2_m_header:
        {
            for(auto str : m_header){
//                QString first = str.left(str.indexOf(":")).trimmed();
//                QString second = str.right(str.length() - str.indexOf(":") - 1).trimmed();
                *header = curl_slist_append(reinterpret_cast<curl_slist*>(*header), str.toStdString().c_str());
            }
            //qDebug() << m_header;
            break;
        }
        default:
            break;
        }
    }
signals:
private:
    long				m_lHttpCode;			//http 响应码
    std::string				m_strHeader;			//响应头
    std::string				m_strContent;			//响应内容
    bool m_bReturn = false;
    QList<QNetworkCookie> m_cookieList;
};

#endif // YCURL_H

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值