CURL中的Post方法 [英] CURL Post method in c++

查看:695
本文介绍了CURL中的Post方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个像下面的HTTP请求:



<预类=郎无prettyprint-覆盖> POST /安全浏览/下载客户端= Firefox和放大器; appver = 3.0.8&安培; PVER = 2.2&安培; WRKEY = AKEgNiux-3bBzAgJeFWgqbneh_GLc2OrmgXnpxPrdH1-hFpbAM8k1ovPA8GB_UMRueBHnL3QJ7gsdQOWVm6QJr_VZNgAm8jmLQ == HTTP / 1.1
主机: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
接受:text / html的,是application / xhtml + XML,应用程序/ 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
连接:keep-alive
Content-Length:120
-Type:text / plain
Cookie:PREF = ID = 551a1b8848e36099:U = e6fa7464d7c48884:FF = 0:TM = 1327553284:LM = 1345022478:S = Qd0IssyrqLi17a4s; NID = 62 = R9Y5bkQ5iLF8zlyhma1gnRBfxPDoWuG2FibC2wc5u0eAIQgAuo4WCXMeLhdPZ7FXJEpN2Sw1a6da6QUNP7OC5OqTYK0Y39vd6c2fUh4BhY2B5CGsKtHuQ5RCpSnefSkb

goog-恶意软件shavar;一个:83372-91327:S:59904-95254:MAC
goog网络钓鱼-shavar; A:227421-233955,235041- 235401:s:107142-110470:mac

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


解决方案

FYI:卷曲规定,列出了特设的选择对于一个给定的HTTP请求一个非常方便的选择:

   -  libcurl< file&转储的libcurl此命令行

在有疑问时,你应该通过卷曲执行你的要求(以及相应的等价代码选项,例如执行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);
...


Suppose we have a HTTP request like following:

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. Does anyone know what parameter I should use to handle message-body using curl_easy_setopt() function?

解决方案

FYI: cURL provides a very convenient option that lists the ad-hoc options for a given HTTP request:

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

When in doubt you should perform your request via cURL (and corresponding options, e.g. to perform a POST, etc) while using this option since it outputs the list of curl_easy_setopt options:

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

Then open the out.c file within your editor:

...
curl_easy_setopt(hnd, CURLOPT_URL, "http://stackoverflow.com/");
curl_easy_setopt(hnd, CURLOPT_PROXY, NULL);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0);
...

这篇关于CURL中的Post方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆