C ++中的CURL Post方法

时间:2012-09-14 05:46:49

标签: c++ libcurl

假设我们有如下的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的解决方案,这不是处理我的数据的方法。有谁知道我应该用什么参数来处理使用curl_easy_setopt()函数的消息体?

2 个答案:

答案 0 :(得分:6)

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

--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);
...

答案 1 :(得分:0)

const char *post_data = "any_data";
...
curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, post_data);