curl 使用范例 c++

7 篇文章 0 订阅

1.

curl_global_init(CURL_GLOBAL_ALL);
m_CurlHandle = curl_easy_init();
//初始化环境,构造handle
  1. 2.
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: */*");
//构造头

3.
回调的函数后面再讲

curl_easy_setopt(m_CurlHandle, CURLOPT_HTTPHEADER, headers);//设置头
curl_easy_setopt(m_CurlHandle, CURLOPT_URL, url);//设置url

curl_easy_setopt(m_CurlHandle, CURLOPT_POSTFIELDSIZE, (long)strlen(content));
curl_easy_setopt(m_CurlHandle, CURLOPT_POSTFIELDS, content);
//设置需要http的内容

curl_easy_setopt(m_CurlHandle, CURLOPT_WRITEDATA, (void *)&m_Chunk);
curl_easy_setopt(m_CurlHandle, CURLOPT_WRITEFUNCTION, RequestCallback);
//设置回调的函数

curl_easy_setopt(m_CurlHandle, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(m_CurlHandle, CURLOPT_CONNECTTIMEOUT_MS, 3000);
curl_easy_setopt(m_CurlHandle, CURLOPT_TIMEOUT_MS, 4000);
//设置一些http请求的参数

CURLcode res = curl_easy_perform(m_CurlHandle);
//执行http调用,res==CURLE_OK 表示调用成功

4.接下来讲一下回调函数

RequestCallback(void *contents, int size, int nmemb, void *userp)
//contents http返回的内容存储在contents数组中
// size * nmemb 是内容的长度
// userp 即curl_easy_setopt配置到m_Chunk中的指针。
  1. 销毁环境
curl_easy_cleanup(m_CurlHandle); 
curl_global_cleanup(); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值