#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;
}
基于libcUrl库HTTP协议封装(2)
最新推荐文章于 2024-05-13 09:47:08 发布