#include "RestClient.h"
#include"curl.h"
CHttpClient::CHttpClient(void) :
m_bDebug(false)
{
//curl_global_init(CURL_GLOBAL_ALL);
}
CHttpClient::~CHttpClient(void)
{
//curl_global_cleanup(); //liburl使用结束,手动close
}
static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
{
if(itype == CURLINFO_TEXT)
{
//printf("[TEXT]%s\n", pData);
}
else if(itype == CURLINFO_HEADER_IN)
{
printf("[HEADER_IN]%s\n", pData);
}
else if(itype == CURLINFO_HEADER_OUT)
{
printf("[HEADER_OUT]%s\n", pData);
}
else if(itype == CURLINFO_DATA_IN)
{
printf("[DATA_IN]%s\n", pData);
}
else if(itype == CURLINFO_DATA_OUT)
{
printf("[DATA_OUT]%s\n", pData);
}
return 0;
}
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
if( NULL == str || NULL == buffer )
{
return -1;
}
char* pData = (char*)buffer;
str->append(pData, size * nmemb);
return nmemb;
}
int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
{
CURLcode res;
//CURL* curl = CurlMutiTreadMutex::GetInstance()->muti_curl_easy_init();
CURL* curl=curl_easy_init();
if(NULL == curl)
{
return CURLE_FAILED_INIT;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
//CurlMutiTreadMutex::GetInstance()->muti_curl_easy_cleanup(curl);
return res;
}
int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
{
int res;
//CURL* curl = CurlMutiTreadMutex::GetInstance()->muti_curl_easy_init();
CURL* curl = curl_easy_init();
if (NULL == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
/* enable TCP keep-alive for this transfer */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
/* keep-alive idle time to 120 seconds */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
/* interval time between keep-alive probes: 60 seconds */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 20L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
/**
* 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
*/
//curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 20);
res = curl_easy_perform(curl);
if (res != 0)
{
//FIRE_ERROR(" Get error %d", res);
}
//CurlMutiTreadMutex::GetInstance()->muti_curl_easy_cleanup(curl);
curl_easy_cleanup(curl);
return res;
}
int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
{
CURLcode res;
//CURL* curl = CurlMutiTreadMutex::GetInstance()->muti_curl_easy_init();
CURL* curl = curl_easy_init();
if(NULL == curl)
{
return CURLE_FAILED_INIT;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
if (strPost!="")
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strPost.size());
}
//增加HTTP header
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "application/json, text/plain, */*");
headers = curl_slist_append(headers, "Content-Type:application/json");
headers = curl_slist_append(headers, "charset:utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
if(NULL == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
//缺省情况就是PEM,所以无需设置,另外支持DER
//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
}
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
res = curl_easy_perform(curl);
if (headers!=NULL)
{
curl_slist_free_all(headers);
}
//CurlMutiTreadMutex::GetInstance()->muti_curl_easy_cleanup(curl);
curl_easy_cleanup(curl);
return res;
}
int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
{
CURLcode res;
//CURL* curl = CurlMutiTreadMutex::GetInstance()->muti_curl_easy_init();
CURL* curl = curl_easy_init();
if(NULL == curl)
{
return CURLE_FAILED_INIT;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
if(NULL == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
}
/* enable TCP keep-alive for this transfer */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
/* keep-alive idle time to 120 seconds */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);
/* interval time between keep-alive probes: 60 seconds */
curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 20L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
res = curl_easy_perform(curl);
//CurlMutiTreadMutex::GetInstance()->muti_curl_easy_cleanup(curl);
curl_easy_cleanup(curl);
return res;
}
///////////////////////////////////////////////////////////////////////////////////////////////
void CHttpClient::SetDebug(bool bDebug)
{
m_bDebug = bDebug;
}
int CHttpClient::GlobleInit()//全局初始化,主程序调用一次
{
return curl_global_init(CURL_GLOBAL_ALL);
}
void CHttpClient::GlobleFint()
{
curl_global_cleanup();
}
int CHttpClient::GetsWithCookie(const std::string & strUrl, std::string & strResponse, std::string& strCookie, const char * pCaPath)
{
CURLcode res;
//CURL* c
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
项目中需要用到Curl频繁调用的情况,发现curl接口调用速度缓慢。为了实现curl高性能,高并发,需要研究如何实现高性能高并发。研究方向有三个。 (1) 长连接。考虑采用长连接的方式去开发。首先研究下长连接和短连接的性能区别。curl内部是通过socket去连接通讯。socket每次连接最为耗时,如果能够复用连接,长时间连接,减少每次socket连接的时间,则可以大大减少时间,提高效率。 (2) 多线程。单个线程下载速度毕竟有限,使用多线程去调用接口。实现高并发高性能,需要考虑资源分配和冲突的问题。 (3) 异步调用。和socket异步调用的原理类似。同步调用会阻塞等待,造成CPU占用率高。
资源推荐
资源详情
资源评论
收起资源包目录
CurlHighSpeed.rar (79个子文件)
CurlHighSpeed
bin
CurlHighSpeed.sln 1KB
CurlHighSpeed
CurlHighSpeed.vcxproj.filters 1KB
Release
RestClientPool.obj 19KB
RestClient.obj 13KB
CurlHighSpeed.log 100B
main.obj 10KB
CurlHighSpeed.tlog
CL.write.1.tlog 1KB
CurlHighSpeed.lastbuildstate 193B
link.command.1.tlog 2KB
CL.read.1.tlog 112KB
CL.command.1.tlog 3KB
link.write.1.tlog 490B
link.read.1.tlog 4KB
RestClient.h 2KB
Resources
RestClient.cpp 12KB
GeneratedFiles
RestClientPool.h 2KB
CurlHighSpeed.vcxproj 9KB
main.cpp 756B
x64
Release
RestClientPool.obj 31KB
CurlHighSpeed.log 3KB
main.obj 2KB
CurlHighSpeed.tlog
link-rc.write.1.tlog 2B
CL.write.1.tlog 824B
CurlHighSpeed.lastbuildstate 191B
link-cvtres.write.1.tlog 2B
link.command.1.tlog 2B
CL.read.1.tlog 60KB
link-cvtres.read.1.tlog 2B
CL.command.1.tlog 2KB
unsuccessfulbuild 0B
link-rc.read.1.tlog 2B
link.write.1.tlog 2B
link.read.1.tlog 2B
Debug
vc140.pdb 764KB
CurlHighSpeed.log 171B
main.obj 55KB
CurlHighSpeed.tlog
CL.write.1.tlog 340B
CurlHighSpeed.lastbuildstate 189B
link.command.1.tlog 1KB
CL.read.1.tlog 22KB
CL.command.1.tlog 864B
unsuccessfulbuild 0B
link.write.1.tlog 446B
link.read.1.tlog 4KB
RestClientPool.cpp 11KB
CurlHighSpeed.vcxproj.user 1KB
.vs
CurlHighSpeed
v14
.suo 34KB
include
curl
system.h 19KB
easy.h 4KB
stdcheaders.h 1KB
urlapi.h 4KB
typecheck-gcc.h 41KB
mprintf.h 2KB
multi.h 17KB
curl.h 111KB
curlver.h 3KB
lib
libcurl_imp.lib 18KB
libssl.lib 119KB
libnghttp2.lib 46KB
Win32
Release
Qt5Widgets.dll 4.22MB
libnghttp2.dll 114KB
libcrypto-1_1.dll 2.27MB
Qt5Svg.dll 259KB
CurlHighSpeed.exe 17KB
Qt5PrintSupport.dll 260KB
Qt5Gui.dll 4.96MB
libssl-1_1.dll 481KB
Qt5Xml.dll 147KB
libcurl.dll 978KB
Qt5Network.dll 961KB
Qt5Core.dll 4.65MB
x64
Debug
libnghttp2.dll 114KB
libcrypto-1_1.dll 2.27MB
CurlHighSpeed.exe 39KB
CurlHighSpeed.pdb 940KB
CurlHighSpeed.ilk 236KB
libssl-1_1.dll 481KB
libcurl.dll 978KB
CurlHighSpeed.VC.db 35.09MB
共 79 条
- 1
资源评论
- JzrSoft2021-02-03优化的curl代码没附上哟
bclshuai
- 粉丝: 97
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计算机网络四次实验报告
- (175549404)基于微信小程序的十二神鹿点餐(外卖小程序)(毕业设计,包括数据库,源码,教程).zip
- (179941432)基于MATLAB车牌识别系统【GUI含界面】.zip
- (179941434)基于MATLAB车牌识别系统【含界面GUI】.zip
- (178021462)基于Javaweb+ssm的医院在线挂号系统的设计与实现.zip
- (178047214)基于springboot图书管理系统.zip
- 张郅奇 的Python学习过程
- (23775420)欧姆龙PLC CP1H-E CP1L-E CJ2M CP1E 以太网通讯.zip
- (174590622)计算机课程设计-IP数据包解析
- (175550824)泛海三江全系调试软件PCSet-All2.0.3 1
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功