我是靠谱客的博主 跳跃萝莉,这篇文章主要介绍C++ 使用curl post 带中文 json数据给服务器,现在分享给大家,希望可以做个参考。

这个坑陷进去好久,网上找了好多办法都没解决中文乱码服务器。工程是多字符集。

一开始按网上例子做,如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int PostData(CString postdata, CStringA url,string & sErro) { TRACE(postdata); TRACE(_T("n**************************************n")); if(!url) {     sErro+=_T("服务器地址为空");          return -1;     }      CURL* curl = NULL;     CURLcode res;     struct curl_slist* headers = NULL;     headers = curl_slist_append(headers, "Accept: application/json");     headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");     curl_global_init(CURL_GLOBAL_ALL);     curl = curl_easy_init();     if(curl) { curl_easy_setopt(curl, CURLOPT_URL,url); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5); //超时时间5s curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5); //超时时间5s curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &sErro);//设置写数据 res = curl_easy_perform(curl); if(res!=0){ CStringA strE=curl_easy_strerror(res); sErro+=strE; } curl_slist_free_all(headers); curl_easy_cleanup(curl); return res; }else{ curl_slist_free_all(headers); sErro+=_T("curl初始化失败!"); return -2; } }

如果json里没有中文,上面一点问题都没有,带中文上面就不行了,服务器返回一长穿错误信息

复制代码
1
复制代码
1
2
{"timestamp":"20180613T07:53:43.879+0000","status":400, "error":"BadRequest","message":"JSON parse error: Invalid UTF-8 middle byte 0xf4n
复制代码
1
2
3
at [Source: (PushbackInputStream); line: 1,column:102]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0xf4n at [Source: (PushbackInputStream); line: 1, column:
复制代码
1
2
3
4
5
102]n at [Source: (PushbackInputStream); line: 1, column: 84] (through reference chain: com.yintong.huihong.api.maintenance. model.MaintenanceDTO["operResult"])","path":"/maintenance/upload/"} 

字符编码问题,转码就好了,一开始用GBK转UTF-8,没用,最后把参数CStringA postdata换成std::string postdata,再转utf-8成功了,用下面这个函数https://blog.csdn.net/yinshi_blog/article/details/6731809

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string string_To_UTF8(const std::string & str) { int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 ZeroMemory(pwBuf, nwLen * 2 + 2); ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen); int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL); char * pBuf = new char[nLen + 1]; ZeroMemory(pBuf, nLen + 1); ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL); std::string retStr(pBuf); delete []pwBuf; delete []pBuf; pwBuf = NULL; pBuf = NULL; return retStr; }
post数据前转化一下
复制代码
1
2
string mJsonData =string_To_UTF8((string)postdata); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, mJsonData.c_str());

最后

以上就是跳跃萝莉最近收集整理的关于C++ 使用curl post 带中文 json数据给服务器的全部内容,更多相关C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部