使用libcurl post json数据,当包含中文时中文会乱码,改用的方法 header 设置 也用过了 还是乱码,代码如下:
CURLcode Curl::sendData(const std::string ¤tUrl,const std::string data)
{
CURLcode res;
struct curl_slist *head = NULL;
head = curl_slist_append(head, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
//head = curl_slist_append(NULL, "Content-Type: application/x-www-form-urlencoded; charset=GB2312");
std::string outData = packetData(data);
//std::string outData = "[{ \"headers\" :{\"namenode\" : \"in header1 ,namenode.example.com\",\"datanode\" : \"in header2,random_datanode.example.com\"},\"body\" : \"haha,this is body\"}]";
//printf("after json:%s\n",outData.c_str());
curl_easy_setopt(m_Curl, CURLOPT_POSTFIELDS,outData.c_str()); // 指定post内容
curl_easy_setopt(m_Curl, CURLOPT_URL, currentUrl.c_str());
//curl_easy_setopt(m_Curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(m_Curl, CURLOPT_HEADER, head);
curl_easy_setopt(m_Curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(m_Curl, CURLOPT_CONNECTTIMEOUT,3);
curl_easy_setopt(m_Curl, CURLOPT_TIMEOUT, 1000);
res = curl_easy_perform(m_Curl);
curl_slist_free_all(head);
return res;
}
------解决思路----------------------
一般来说,含中文的话,都会使用GB2312字符集。