libcurl http发送字符串引起的乱码的解决方案CURLOPT_POSTFIELDSIZE


请求端:
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl_send_jsondata_curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl_send_jsondata_curl, CURLOPT_POSTFIELDSIZE, strlen(data));
curl_easy_setopt(curl_send_jsondata_curl, CURLOPT_POSTFIELDSIZE_LARGE, strlen(data));
curl_easy_setopt(curl_send_jsondata_curl, CURLOPT_POSTFIELDS, data);

接收端:
printf("recv > %.*s",body_len,body_data);

1.使用CURLOPT_POSTFIELDSIZE和CURLOPT_POSTFIELDSIZE_LARGE来指定长度,注意调用顺序,要在CURLOPT_POSTFIELDS之前。
2.由于data类型是数组char[],所以要注意head类型,防止libcurl认为你要发送的是数组。所以需要指定头,例指定:Content-Type: application/x-www-form-urlencoded(我没有加Content-Type就解决了,接收端根据length,打印接收的body即可,我用的是post)
参考:
https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html
https://curl.se/libcurl/c/CURLOPT_POSTFIELDSIZE_LARGE.html

posted @ 2022-08-19 09:44  Smah  阅读(737)  评论(0编辑  收藏  举报