cjson,libcurl 使用demo

参考:

  1,https://blog.csdn.net/qq_58168493/article/details/122537407

   2,curl 获取 https 请求方法_ypbsyy的博客-CSDN博客_curl请求https

   (相关的源码文件都可以在github上下载)

  这些开源的库使用起来能够节省不少精力和时间。如果是自己去实现相关的功能,一方面是时间来不及,一方面是效果和性能不一定有保证。这些库都是经过长时间维护和优化的,在开发中应用确实比较方便(其实是自己做不到同等效果,哈哈)。

使用例子

1,cjson是一个关于json数据使用的库,详细介绍见参考1,demo如下:

VOID UseJson(char* data1)
{
	cJSON* cjson_test = NULL;
	cJSON* cjson_addr = NULL;
	cJSON* cjson_host_item = NULL;
	cJSON* cjson_hostarray = NULL;

	//创建一根json数据对象(链表头节点)
	cjson_test = cJSON_CreateObject();

	//添加一条字符串类型的json数据(添加一个链表节点)
	cJSON_AddStringToObject(cjson_test, "mac", "01-02-03-04-05");
	cJSON_AddStringToObject(cjson_test, "token", "kalsdjfkasuroieuiroweeur");
	cJSON_AddStringToObject(cjson_test, "ip", "127.0.0.10");
	cJSON_AddStringToObject(cjson_test, "stime", "2022-11-11,11:19:33");
	cJSON_AddStringToObject(cjson_test, "sverion", "10.0.19041,1778");
	cJSON_AddStringToObject(cjson_test, "country", "中国");

	cJSON_AddStringToObject(cjson_test, "data1", data1);


	cjson_addr = cJSON_CreateObject();
	//添加一条整数类型的json数据(添加一个链表节点)
	cJSON_AddNumberToObject(cjson_addr, "stage", 3);
	cJSON_AddNumberToObject(cjson_addr, "state", 2);
	//添加一个嵌套的json数据
	cJSON_AddItemToObject(cjson_test, "sstage", cjson_addr);

	//添加一个数组类型的json数据(添加一个链表节点)
	cjson_hostarray = cJSON_CreateArray();

	//添加一个节点到数组中
	cjson_host_item = cJSON_CreateObject();
	cJSON_AddStringToObject(cjson_host_item, "host", "www.baidu.com");
	cJSON_AddNumberToObject(cjson_host_item, "type", 2);
	cJSON_AddItemToArray(cjson_hostarray, cjson_host_item);

	//
	cjson_host_item = cJSON_CreateObject();
	cJSON_AddStringToObject(cjson_host_item, "host", "www.intel.com");
	cJSON_AddNumberToObject(cjson_host_item, "type", 1);
	cJSON_AddItemToArray(cjson_hostarray, cjson_host_item);

	cJSON_AddItemToObject(cjson_test, "hostarray", cjson_hostarray);

	//将json数据转换为非格式化的字符串
	char* str = cJSON_PrintUnformatted(cjson_test);
	printf("json is %s\n", str);

	cJSON_Delete(cjson_test);
}

  结果如下,在线json解析:https://www.sojson.com/

{
	"mac": "01-02-03-04-05",
	"token": "kalsdjfkasuroieuiroweeur",
	"ip": "127.0.0.10",
	"stime": "2022-11-11,11:19:33",
	"sverion": "10.0.19041,1778",
	"country": "中国",
	"data1": "pass",
	"sstage": {
		"stage": 3,
		"state": 2
	},
	"hostarray": [{
		"host": "www.baidu.com",
		"type": 2
	}, {
		"host": "www.intel.com",
		"type": 1
	}]
}

2,libcurl是一个网络相关的库,支持多种编译选项,可根据实际需求生成不同平台的lib或dll。下面是通过libcurl下载文件:

size_t recvd = 0;
static size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream)
{
	//printf("wnter write_data!\n");
	size_t written = fwrite(ptr, size, nmemb, (FILE*)stream);

	recvd += written;
	return written;
}

//urlFileName 需要下载文件的url
//localFileName  保存到本地文件
//测试 //"https://sourceforge.net/projects/processhacker/files/processhacker2/processhacker-2.39-setup.exe";
ULONG downLoadFile(char* urlFileName,char* localFilename)
{
	CURL* curl_handle;
	char* pagefilename = localFilename;
	FILE* pagefile;
	char* p = (char*)urlFileName;
	ULONG result = 0;

	 
	curl_global_init(CURL_GLOBAL_ALL);

	/* init the curl session */
	curl_handle = curl_easy_init();

	/* set URL to get here */
	curl_easy_setopt(curl_handle, CURLOPT_URL, p);

	/* Switch on full protocol/debug output while testing */
	curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

	/* disable progress meter, set to 0L to enable and disable debug output */
	curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
	/* google.com is redirected, so we tell LibCurl to follow redirection */
	curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
	/* SSL Options */
	//do not check certs
	curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER,0 );
	curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);

	/* Provide CA Certs from http://curl.haxx.se/docs/caextract.html */
	//curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "ca-bundle.crt");
	/* send all data to this function  */
    //在write_data函数中处理接收的所有有效数据
    //write_data相当于一个回调函数,当接收到数据就处理一次
	curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

	//set user agent
	curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Dark Secret Ninja/1.0");

	/* open the file */
	pagefile = fopen(localFilename, "wb");
	if (pagefile) {

		/* write the page body to this file handle */
		curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

		CURLE_OK;
		CURLE_SSL_CONNECT_ERROR;
		/* get it! */
		printf("result:%d\n",curl_easy_perform(curl_handle));

		/* close the header file */
		fclose(pagefile);
	}
	else
	{
		//fail to create new file
		result = 1;
	}

	/* cleanup curl stuff */
	curl_easy_cleanup(curl_handle);
	return result;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值