c++ curl使用ip4_c++使用curl库访问服务器并获取应答结果

size_t http_data_writer(void* data, size_t size, size_t nmemb, void* content)

{

long totalSize = size*nmemb;

std::string* symbolBuffer = (std::string*)content;

if(symbolBuffer)

{

symbolBuffer->append((char *)data, ((char*)data)+totalSize);

}

return totalSize;

}

bool AccessWeb(char* szUrl)

{

CURL* curl = NULL;

curl=curl_easy_init();

CURLcode code;

// 设置Url

code = curl_easy_setopt(curl, CURLOPT_URL, szUrl);

// 设置post的json数据

char szPost[64] = "msg=hello";

code = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, szPost);

// 设置回调函数

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);

//设置写数据

std::string strData;

curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strData);

// 执行请求

code = curl_easy_perform(curl);

if(code == CURLcode::CURLE_OK)

{

long responseCode = 0;

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);

if (responseCode < 200 || responseCode >= 300 || strData.empty())

{

return false;

}

//下面可以对应答的数据进行处理了

// strData

}

// 清除curl对象

curl_easy_cleanup(curl);

return true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值