相关文章推荐
讲道义的凉面  ·  郑州大学信息管理学院·  3 周前    · 
豁达的小摩托  ·  于晓非_百度百科·  2 月前    · 
阳刚的紫菜汤  ·  科学精神_百度百科·  10 月前    · 
成熟的小熊猫  ·  吕鹏:人工智能伦理问题需实证证实或证伪| ...·  1 年前    · 
爱搭讪的桔子  ·  2023年10大考研机构排名(仅供参考) - 知乎·  2 年前    · 
Code  ›  C++使用curl发送post请求开发者社区
string post curl
https://cloud.tencent.com/developer/article/2110260
性感的小蝌蚪
2 年前
作者头像
全栈程序员站长
0 篇文章

C++使用curl发送post请求

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 全栈程序员必看 > 正文

C++使用curl发送post请求

发布 于 2022-09-18 14:56:04
824 0
举报

大家好,又见面了,我是你们的朋友全栈君。

发送post请求代码如下:

#include<iostream>
#include<string>
#include<curl\curl.h>
using namespace std;
//get请求和post请求数据响应函数
size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream)
	//在注释的里面可以打印请求流,cookie的信息
	//cout << "----->reply" << endl;
	string *str = (string*)stream;
	//cout << *str << endl;
	(*str).append((char*)ptr, size*nmemb);
	return size * nmemb;
//http POST请求 
CURLcode curl_post_req(const string &url, const string &postParams, string &response)
	// curl初始化 
	CURL *curl = curl_easy_init();
	// curl返回值 
	CURLcode res;
	if (curl)
		// set params
		//设置curl的请求头
		struct curl_slist* header_list = NULL;
		header_list = curl_slist_append(header_list, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
		header_list = curl_slist_append(header_list, "Content-Type:application/x-www-form-urlencoded; charset=UTF-8");
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
		//不接收响应头数据0代表不接收 1代表接收
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);
		//设置请求为post请求
		curl_easy_setopt(curl, CURLOPT_POST, 1);
		//设置请求的URL地址
		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
		//设置post请求的参数
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postParams.c_str());
		//设置ssl验证
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
		//CURLOPT_VERBOSE的值为1时,会显示详细的调试信息
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
		curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
		//设置数据接收和写入函数
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
		curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
		//设置超时时间
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 6);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 6);
		// 开启post请求
		res = curl_easy_perform(curl);
	//释放curl 
	curl_easy_cleanup(curl);
	return res;
int main()
	string url_post0 = "http://httpbin.org/post";
	string paramsLogin0 = "key1=value1&key2=value2";
	string resPost0;
	auto res3 = curl_post_req(url_post0, paramsLogin0, resPost0);
	if (res3 == CURLE_OK)
		cout << resPost0 << endl;
 
推荐文章
讲道义的凉面  ·  郑州大学信息管理学院
3 周前
豁达的小摩托  ·  于晓非_百度百科
2 月前
阳刚的紫菜汤  ·  科学精神_百度百科
10 月前
成熟的小熊猫  ·  吕鹏:人工智能伦理问题需实证证实或证伪| 人工智能伦理与治理专题 ...
1 年前
爱搭讪的桔子  ·  2023年10大考研机构排名(仅供参考) - 知乎
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号