#ifndef YCURL_H
#define YCURL_H
#include
#include "YCHttp.h"
#include
#include
#include
#include
#include
#include
#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();
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);
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 bools_bInit;//是否初始化
static CURLSH*s_pShareHandle;//共享curl句柄
QMutex m_mutex;
CURL*m_hCurl;//easycurl句柄
QStringList m_header;
void setHeader(curl_slist **header, YCHeaderIndex nIndex)
{
switch(nIndex)
{
case URL2_None:
{
*header = curl_slist_append(reinterpret_cast(*header), "");
break;
}
case URL2_GetDefaut:
*header = curl_slist_append(reinterpret_cast(*header), "User-Agent:Mozilla/5.0");
break;
case URL2_PostDefaut:
*header = curl_slist_append(reinterpret_cast(*header), "Content-Type:application/x-www-form-urlencoded; charset=UTF-8");
break;
case URL2_PostJson:
*header = curl_slist_append(reinterpret_cast(*header), "Content-Type:application/json");
break;
case URL2_PostXml:
*header = curl_slist_append(reinterpret_cast(*header), "Content-Type:application/xml");
break;
case URL2_PostZip:
*header = curl_slist_append(reinterpret_cast(*header), "Content-Type:application/zip");
break;
case URL2_PostPlainText:
*header = curl_slist_append(reinterpret_cast(*header), "Content-Type:text/plain");
break;
case URL2_PostHtml:
*header = curl_slist_append(reinterpret_cast(*header), "Content-Type:text/html");
break;
case URL2_PostPng:
*header = curl_slist_append(reinterpret_cast(*header), "Content-Type:image/png");
break;
case URL2_PostJpg:
*header = curl_slist_append(reinterpret_cast(*header), "Content-Type:image/jpg");
break;
case URL2_VerifyCode: //12306
{
//qDebug() << "12306pppp";
*header = curl_slist_append(reinterpret_cast(*header), "Referer: https://kyfw.12306.cn/otn/login/init");
*header = curl_slist_append(reinterpret_cast(*header), "Connection: keep-alive");
*header = curl_slist_append(reinterpret_cast(*header), "Accept: application/json, text/javascript,*/*; q=0.8");
*header = curl_slist_append(reinterpret_cast(*header), "Accept-Encoding: gzip, deflate, br");
*header = curl_slist_append(reinterpret_cast(*header), "Accept-Language: zh-CN,zh;q=0.8");
*header = curl_slist_append(reinterpret_cast(*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(*header), str.toStdString().c_str());
}
//qDebug() << m_header;
break;
}
default:
break;
}
}
signals:
private:
longm_lHttpCode;//http 响应码
std::stringm_strHeader;//响应头
std::stringm_strContent;//响应内容
bool m_bReturn = false;
};
#endif // YCURL_H