基于libcUrl库HTTP协议封装(2)

#include"libcurl.h"
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* pVoid)
{
	std::string* str = dynamic_cast<std::string*>((std::string*)pVoid);
	if (NULL == str || NULL == buffer)
 	{
  		return 0;
 	}
 	char* pData = (char*)buffer;
 	str->append(pData, size * nmemb);
 	return nmemb;
}
int libHttpPost(const std::string& strUrl, const std::string& strCookie, const std::string& strPost, std::string& strResponse, int nType)
{
	CURRLcode res;
 	CURL* curl = curl_easy_init();
 	if (NULL == curl)
	{
  		return CURLE_FAILED_INIT;
 	}
 	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, TRUE);
 	if (!strUrl.empty())
 	{
  		curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
 	}
 	else
 	{
  		curl_easy_cleanup(curl);
  		return -1;
 	}
 	if (!strCookie.empty())
 	{
  		curl_easy_setopt(curl, CURLOPT_COOKIE, strCookie.c_str());
 	}
 	struct curl_slist* chunk = NULL;
 	chunk = curl_slist_append(chunk, "Accept-Language:zh-CN");
 	curl_easy_setopt(curl, CURLOPT_HTTPEADER, chunk);
 	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);
 	switch (nType)
 	{
 	case HTTP_RESPONSE_HEAD:
 		curl_easy_setopt(curl, CUROLOPT_HEADERDATA, (void*)&strResponse);
  		break;
  	case HTTP_RESPONSE_BODY:
  		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
  		break;
  	case HTTP_RESPONSE_ALL:
  		curl_easy_setopt(curl, CURLOPT_HEADER, TRUE);
  		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
  		break;
  	default:
  		curl_easy_setopt(curl, CURLOPT_HEADER, TRUE);
  		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
 		break;
 	}
 	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, TRUE);
 	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CURL_CONNECT_TIMEOUT);
 	curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_CURLOPT_TIMEOUT);
 	res = curl_easy_perform(curl);
 	curl_easy_cleanup(curl);
 	return res;
}
int libHttpGet(const std::string& strUrl, const std::string& strCookie, std::string& strResponse, int nType)
{
	CURRLcode res;
 	CURL* curl = curl_easy_init();
 	if (NULL == curl)
	{
  		return CURLE_FAILED_INIT;
 	}
 	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, TRUE);
 	if (!strUrl.empty())
 	{
  		curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
 	}
 	else
 	{
  		curl_easy_cleanup(curl);
  		return -1;
 	}
 	if (!strCookie.empty())
 	{
  		curl_easy_setopt(curl, CURLOPT_COOKIE, strCookie.c_str());
 	}
 	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
 	switch (nType)
 	{
 	case HTTP_RESPONSE_HEAD:
  		curl_easy_setopt(curl, CUROLOPT_HEADERDATA, (void*)&strResponse);
  		break;
  	case HTTP_RESPONSE_BODY:
  		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
  		break;
  	case HTTP_RESPONSE_ALL:
  		curl_easy_setopt(curl, CURLOPT_HEADER, TRUE);
  		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
  		break;
  	default:
  		curl_easy_setopt(curl, CURLOPT_HEADER, TRUE);
  		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
  		break;
 	}
 	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, TRUE);
 	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CURL_CONNECT_TIMEOUT);
 	curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_CURLOPT_TIMEOUT);
 	res = curl_easy_perform(curl);
 	curl_easy_cleanup(curl);
 	return res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2020年冬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值