c++中的CURL Post方法

CURL Post method in c++

本文关键字:方法 Post CURL 中的 c++      更新时间:2023-10-16

假设我们有一个HTTP请求,如下所示:

POST /safebrowsing/downloads?client=Firefox&appver=3.0.8&pver=2.2&wrkey=AKEgNiux-3bBzAgJeFWgqbneh_GLc2OrmgXnpxPrdH1-hFpbAM8k1ovPA8GB_UMRueBHnL3QJ7gsdQOWVm6QJr_VZNgAm8jmLQ== HTTP/1.1
Host: safebrowsing.clients.google.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko/2009033100 Ubuntu/9.04 (jaunty) Firefox/3.0.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Length: 120
Content-Type: text/plain
Cookie: PREF=ID=551a1b8848e36099:U=e6fa7464d7c48884:FF=0:TM=1327553284:LM=1345022478:S=Qd0IssyrqLi17a4s; NID=62=R9Y5bkQ5iLF8zlyhma1gnRBfxPDoWuG2FibC2wc5u0eAIQgAuo4WCXMeLhdPZ7FXJEpN2Sw1a6da6QUNP7OC5OqTYK0Y39vd6c2fUh4BhY2B5CGsKtHuQ5RCpSnefSkb
goog-malware-shavar;a:83372-91327:s:59904-95254:mac
goog-phish-shavar;a:227421-233955,235041-235401:s:107142-110470:mac

我已经构建了处理标头的代码部分。然而,消息正文部分我不知道该如何处理。我已经阅读了CURL示例代码,它们为HTTP表单POST提供了解决方案,而POST不是处理我的数据的方法。有人知道我应该使用什么参数来处理使用curl_easy_setopt()函数的消息体吗?

FYI:cURL提供了一个非常方便的选项,列出了给定HTTP请求的特定选项:

--libcurl <file> Dump libcurl equivalent code of this command line

如果有疑问,您应该通过cURL(以及相应的选项,例如执行POST等)执行请求,同时使用此选项,因为它输出curl_easy_setopt选项列表:

curl --libcurl out.c http://stackoverflow.com/ > /dev/null

然后在编辑器中打开out.c文件:

...
curl_easy_setopt(hnd, CURLOPT_URL, "http://stackoverflow.com/");
curl_easy_setopt(hnd, CURLOPT_PROXY, NULL);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0);
...
const char *post_data = "any_data";
...
curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, post_data);