我在使用c++的libcurl库访问网站返回数据出现中文乱码想知道解决办法
程序是
#include <iostream>
#include <curl/curl.h>
#include<windows.h>
#include<string>
;
// 回调函数,用于处理获取到的网页数据
size_t WriteCallback(char* contents, size_t size, size_t nmemb, std::string* response)
{
size_t totalSize = size * nmemb;
response->append(contents, totalSize);
return totalSize;
}
int main()
{
// 初始化curl
curl_global_init(CURL_GLOBAL_DEFAULT);
// 创建curl句柄
CURL* curl = curl_easy_init();
if (curl) {
// 设置要请求的URL
curl_easy_setopt(curl, CURLOPT_URL, "https://comment.bilibili.com/1057102166.xml");
// 设置回调函数,用于处理获取到的网页数据
std::string response;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// 发起请求
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
std::cerr << "请求失败,错误代码: " << res << std::endl;
else
std::cout << response << std::endl;
// 清理curl句柄
curl_easy_cleanup(curl);
}
// 清理全局curl资源
curl_global_cleanup();
return 0;
}