1.
curl_global_init(CURL_GLOBAL_ALL);
m_CurlHandle = curl_easy_init();
//初始化环境,构造handle
- 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中的指针。
- 销毁环境
curl_easy_cleanup(m_CurlHandle);
curl_global_cleanup();