C++ - 如何使用Curlpp或libcurl发送HTTP POST请求

7
我想用C ++发送一个http post请求。似乎libcurl(Curlpp)是正确的选择。 现在,这是我要发送的典型请求。
http://abc.com:3456/handler1/start?<name-Value pairs>

The name values pairs will have:

field1: ABC
field2: b, c, d, e, f
field3: XYZ

etc.

现在,我想知道如何使用curlpp或libcurl实现相同的功能。代码片段会很有帮助。
1个回答

4

我没有使用过Curlpp,但这是我使用libcurl的方法。

您可以使用以下方式设置目标URL:

curl_easy_setopt(m_CurlPtr, CURLOPT_URL, "http://urlhere.com/");

POST值存储在一个链接列表中,你需要有两个变量来保存该列表的开头和结尾,以便cURL可以向其中添加一个值。

struct curl_httppost* beginPostList;
struct curl_httppost* endPostList;

您可以使用以下方法添加此帖子变量:
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, "value", CURLFORM_END);

提交的流程如下所示:
curl_easy_setopt(m_CurlPtr, CURLOPT_POST, true);
curl_easy_setopt(m_CurlPtr, CURLOPT_HTTPPOST, beginPostList); 
curl_easy_perform(m_CurlPtr);

希望这可以帮到你!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接