关于libcurl的相关函数介绍以及参数详见官方说明 https://curl.haxx.se/libcurl/c/example.html
HTTP Request
一个http请求包含方法、路径、http版本、请求包头
请求方法 GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS
GET
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com?id=1")
POST
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "username=admin&password=123");
PUT
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
DELETE
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/file.txt");
libcurl HTTP 请求包头
默认不设置的包头
GET
GET /file1.txt HTTP/1.1
Host: localhost
Accept: */*
POST
POST /file1.txt HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 6
Content-Type: application/x-www-form-urlencoded
设置自定义包头
添加一个包头 Name:Mike
struct curl_slist *list = NULL;
list = curl_slist_append(list, "Name: Mike");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_perform(curl);
curl_slist_free_all(list); /* free the list again */
修改一个包头 Host: Alternative
struct curl_slist *list = NULL;
list = curl_slist_append(list, "Host: Alternative");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_perform(curl);
curl_slist_free_all(list); /* free the list again */
删除一个包头
struct curl_slist *list = NULL;
list = curl_slist_append(list, "Accept:");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_perform(curl);
curl_slist_free_all(list); /* free the list again */
提供一个没有内容的包头
struct curl_slist *list = NULL;
list = curl_slist_append(list, "Moo;");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);