C++ 使用curl post 带中文 json数据给服务器

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

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

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里没有中文,上面一点问题都没有,带中文上面就不行了,服务器返回一长穿错误信息

 
 
{"timestamp":"20180613T07:53:43.879+0000","status":400,
"error":"BadRequest","message":"JSON parse error: Invalid UTF-8 middle byte 0xf4\n 
at [Source: (PushbackInputStream); line: 1,column:102]; 
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
 Invalid UTF-8 middle byte 0xf4\n at [Source: (PushbackInputStream); line: 1, column:
 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

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数据前转化一下
	string mJsonData =string_To_UTF8((string)postdata);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, mJsonData.c_str());

猜你喜欢

转载自blog.csdn.net/ya4599/article/details/80680003