简体   繁体   English

C ++中的CURL Post方法

[英]CURL Post method in c++

Suppose we have a HTTP request like following: 假设我们有一个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

I already built up the part of code to handle the headers. 我已经建立了部分代码来处理标题。 However, the message-body part I'm not sure how to deal with. 但是,消息正文部分我不确定如何处理。 I have read CURL sample code, they provide solution for HTTP form POST which is not the way to handle my data. 我已经阅读了CURL示例代码,它们为HTTP表单POST提供了解决方案,而这不是处理我的数据的方法。 Does anyone know what parameter I should use to handle message-body using curl_easy_setopt() function? 有谁知道我应该使用curl_easy_setopt()函数使用什么参数来处理消息正文?

FYI: cURL provides a very convenient option that lists the ad-hoc options for a given HTTP request: 仅供参考:cURL提供了一个非常方便的选项,它列出了给定HTTP请求的即席选项:

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

When in doubt you should perform your request via cURL (and corresponding options, eg to perform a POST, etc) while using this option since it outputs the list of curl_easy_setopt options: 如有疑问,应在使用此选项时通过cURL(和相应的选项,例如执行POST等)执行请求,因为它会输出curl_easy_setopt选项的列表:

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

Then open the out.c file within your editor: 然后在编辑器中打开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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM