当前位置:网站首页>Compress the curl library into a sending string of utf8 and send it with curl library
Compress the curl library into a sending string of utf8 and send it with curl library
2022-04-23 11:19:00 【Brother dampness】
#include <curl/curl.h>
int my_curl_init(void)
{
curl_global_init(CURL_GLOBAL_ALL);
return 1;
}
void my_curl_end(void)
{
curl_global_cleanup();
return;
}
int my_curl_post_parse(void* buffer, size_t size, size_t nmemb, char * useless)
{
memcpy(useless+strlen(useless), (char *)buffer, size*nmemb);
//if (1 == tunable_nd_debug_http_reponsed)
// printf("-------%s\n", useless);
return size*nmemb;
}
int my_curl_post_get(char *send_data, const char *url)
{
(void) nWriteLog;
if (!send_data)
return 0;
CURLcode res;
CURL *curl;
int iRet = 1;
char szBuf[2048];
struct curl_slist *chunk =0;
curl = curl_easy_init();
if (!curl)
return 0;
char* send_data_urlencode = curl_easy_escape(curl, send_data, strlen(send_data));
if (!send_data_urlencode)
{
curl_free(send_data_urlencode);
curl_easy_cleanup(curl);
return 0;
}
int nTotalLen = strlen(send_data_urlencode);
int nOffset = 0;
if (nTotalLen < 1)
{
curl_easy_cleanup(curl);
return 0;
}
for (int i = 0; i < nTotalLen; i++)
{
char v = send_data_urlencode[i];
switch (v)
{
case '%':
if ((i + 2) < nTotalLen)
{
if ('3' == send_data_urlencode[i + 1])
{
if ('D' == send_data_urlencode[i + 2])
{
send_data_urlencode[nOffset] = '=';
i += 2;
nOffset++;
continue;
}
}
else if ('2' == send_data_urlencode[i + 1])
{
if ('6' == send_data_urlencode[i + 2])
{
send_data_urlencode[nOffset] = '&';
i += 2;
nOffset++;
continue;
}
}
}
break;
default:
break;
}
send_data_urlencode[nOffset] = v;
nOffset++;
}
send_data_urlencode[nOffset] = 0;
chunk = curl_slist_append(chunk, "Content-Type: application/x-www-form-urlencoded");
sprintf(szBuf, "Content-Length: %ld", strlen(send_data_urlencode));
chunk = curl_slist_append(chunk, szBuf);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, send_data_urlencode);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_curl_post_parse);
memset(szBuf, 0, sizeof(szBuf));
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&szBuf);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
long httpcode = 0;
res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
if (CURLE_OK == curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode))
{
plog("httpcode=%ld", httpcode);
}
}
int nSuccess = 0;
if (strstr(szBuf, "\"Code\":13000"))
{
nSuccess=1;
}
if (200 == httpcode)
{
iRet = 1;
if ( 0 == nSuccess)
{
}
}
else
{
iRet = -1;
}
curl_free(send_data_urlencode);
curl_easy_cleanup(curl);
return iRet;
}
The code called is as follows :
my_curl_init();
int nRet = my_curl_post_get( SendDataBuf, tunable_nd_log_url);
my_curl_end();
版权声明
本文为[Brother dampness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231115090984.html
边栏推荐
- Oracle connectivity test gadget
- nacos基础(6):nacos配置管理模型
- MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video
- Database management software sqlpro for SQLite for Mac 2022.30
- 面向全球市场,PlatoFarm今日登录HUOBI等全球四大平台
- laravel-admin表单验证
- 卷积层和池化层总结
- nacos基础(7):配置管理
- MySQL8.0升级的踩坑历险记
- About the three commonly used auxiliary classes of JUC
猜你喜欢
Constraintlayout layout
Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了
MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video
Mysql8.0安装指南
C#的学习笔记【八】SQL【一】
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
年度最尴尬的社死瞬间,是Siri给的
语雀文档编辑器将开源:始于但不止于Markdown
Get things technology network optimization - CDN resource request Optimization Practice
随机推荐
活动进行时! 点击链接加入直播间参与“AI真的能节能吗?”的讨论吧!
Detailed explanation of how to smoothly go online after MySQL table splitting
学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
学习 Go 语言 0x03:理解变量之间的依赖以及初始化顺序
Analysis on the characteristics of the official game economic model launched by platoffarm
Constraintlayout layout
nacos基础(9):nacos配置管理之从单体架构到微服务
Mysql中一千万条数据怎么快速查询
mysql分表之后如何平滑上线详解
On lambda powertools typescript
Mba-day5 Mathematics - application problems - engineering problems
Mysql database transaction example tutorial
QT 怎么把QWigdet变成QDialog
学习 Go 语言 0x01:从官网开始
语雀文档编辑器将开源:始于但不止于Markdown
讯飞2021年营收183亿:同比增41% 净利为15.56亿
解读机器人创造出来的艺术