使用curl库,以post方式向服务器发送json数据

//使用curl库,以post方式向服务器发送json数据
//json数据的组合可以参考jsoncpp库,也可以按json格式自己组合字符串

//注意事项,以下代码不可以多线程执行,如果多线程执行,需要加锁进行控制,否则会运行崩溃

 

#include <curl/curl.h>
#include <string>
#include <exception>

int main(int argc, char *argv[]) 
{
	char szJsonData[1024];
	memset(szJsonData, 0, sizeof(szJsonData));
	std::string strJson = "{";
	strJson += "\"user_name\" : \"test\",";
	strJson += "\"password\" : \"test123\"";
	strJson += "}";
	strcpy(szJsonData, strJson.c_str());
	try 
	{
		CURL *pCurl = NULL;
		CURLcode res;
		// In windows, this will init the winsock stuff
		curl_global_init(CURL_GLOBAL_ALL);

		// get a curl handle
		pCurl = curl_easy_init();
		if (NULL != pCurl) 
		{
			// 设置超时时间为1秒
			curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 1);

			// First set the URL that is about to receive our POST. 
			// This URL can just as well be a 
			// https:// URL if that is what should receive the data.
			curl_easy_setopt(pCurl, CURLOPT_URL, "http://192.168.0.2/posttest.svc");
			//curl_easy_setopt(pCurl, CURLOPT_URL, "http://192.168.0.2/posttest.cgi");

			// 设置http发送的内容类型为JSON
			curl_slist *plist = curl_slist_append(NULL, 
				"Content-Type:application/json;charset=UTF-8");
			curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);

			// 设置要POST的JSON数据
			curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, szJsonData);

			// Perform the request, res will get the return code 
			res = curl_easy_perform(pCurl);
			// Check for errors
			if (res != CURLE_OK) 
			{
				printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res));
			}
			// always cleanup
			curl_easy_cleanup(pCurl);
		}
		curl_global_cleanup();
	}
	catch (std::exception &ex)
	{
		printf("curl exception %s.\n", ex.what());
	}
	return 0;
}

 

参考文档curl压缩包\docs\examples\http-post.c

curl压缩包下载地址:http://curl.haxx.se/download.html


 

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我可以帮您解答这个问题。以下是一个使用boost生成json文件并用curlpost请求向服务器发送json的示例代码: ```cpp #include <iostream> #include <fstream> #include <string> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <curl/curl.h> using namespace std; using namespace boost::property_tree; // 回调函数,用于接收服务器返回的数据 size_t WriteCallback(char* contents, size_t size, size_t nmemb, void* userp) { ((string*)userp)->append(contents, size * nmemb); return size * nmemb; } int main() { // 创建一个ptree,用于存储json数据 ptree pt; pt.put("name", "张三"); pt.put("age", 18); // 将ptree中的数据转换成json格式的字符串 ostringstream buf; write_json(buf, pt, false); string json = buf.str(); cout << "json: " << json << endl; // 使用curl服务器发送post请求 CURL* curl; CURLcode res; string response; curl = curl_easy_init(); if (curl) { // 设置post请求的url curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/api"); // 设置post请求的数据 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str()); // 设置post请求的数据类型为json struct curl_slist* headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 设置接收服务器返回数据的回调函数 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); // 执行post请求 res = curl_easy_perform(curl); // 清理工作 curl_easy_cleanup(curl); curl_slist_free_all(headers); } // 输出服务器返回的数据 cout << "response: " << response << endl; return 0; } ``` 希望能对您有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值