本文主要是介绍libcurl读取本地文件,文件名带有中文,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.路径无中文
std::string respBodyData;
std::string respHeadData;
CURL* curl;
CURLcode res;// 日志记录请求内容
Logger::HttpInfo(constant, "无请求参数");// 设置头
struct curl_slist *headers = NULL;
// 初始化libcurl
curl_global_init(CURL_GLOBAL_ALL);// 创建一个CURL句柄
curl = curl_easy_init();
if (curl == NULL)
{MessageBox(NULL, L"错误的网络连接!", L"错误", MB_ICONERROR);return S_FALSE;
}// 读取请求地址
std::string url;
ReadContantFromFile("addr", url, GetCredentialsFilePath);
FILE *file = fopen(filePath.c_str(), "rb");
if (!file) {MessageBox(NULL, L"文件打开失败,请联系管理员!", L"错误", MB_ICONERROR);return S_FALSE;
}
// 读取文件,由于libcurl不支持unicode格式,采用的ascii编码,因此采用回调函数的形式
size_t pos = filePath.rfind('\\');
std::string fileNameGbk = filePath.substr(pos + 1);
std::string fileNameUtf8;
GbkToUtf8(fileNameGbk, fileNameUtf8);
curl_mime *mime = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(mime);
curl_mime_name(part, "files");
ULONGLONG fileSize = GetFileSize(filePath);
curl_mime_filename(part, filePath.c_str());
curl_mime_filename(part, fileNameUtf8.c_str());
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_URL, (url + mapping).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WritePostHeaderResp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WritePostBodyResp);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &respHeadData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respBodyData);// curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 5000); //libcurl存在毫秒超时bug,如果设备小于1000ms立即返回失败
// curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 5000); //设置超时时间bool bCA = FALSE;
if (!bCA)
{curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);//设定为不验证证书和HOST curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
else
{curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, TRUE);curl_easy_setopt(curl, CURLOPT_CAINFO, "");
}res = curl_easy_perform(curl);
fclose(file);
if (res != CURLE_OK)
{MessageBox(NULL, CA2W(curl_easy_strerror(res)), L"错误", MB_ICONEXCLAMATION);return S_FALSE;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_mime_free(mime);
std::string respBodyDataGbk;
Utf8ToGbk(respBodyData, respBodyDataGbk);
std::string result = respBodyDataGbk.c_str();
// 日志记录返回值
Logger::HttpInfo(constant, respBodyDataGbk, false);// 处理返回值
return ResultHandler(result, data);
2.路径有中文
size_t ReadCallback(char *buffer, size_t size, size_t nitems, void *userdata) {FILE *file = (FILE *)userdata;return fread(buffer, size, nitems, file);;
}
int HttpPost(const std::string constant, const char * mapping, Json::Value & data, BOOL isVerify, const std::string filePath)
{std::string respBodyData;std::string respHeadData;CURL* curl;CURLcode res;// 日志记录请求内容Logger::HttpInfo(constant, "无请求参数");// 设置头struct curl_slist *headers = NULL;// 设置Tokenif (isVerify){// 读取Tokenstd::string token;ReadContantFromFile("token", token, GetCredentialsFilePath);if (token.empty()){MessageBox(NULL, L"当前登录已失效,请重新登录", L"错误", MB_ICONEXCLAMATION);return S_FALSE;}// 设置token请求头headers = curl_slist_append(headers, ("Authorization:" + token).c_str());}// 初始化libcurlcurl_global_init(CURL_GLOBAL_ALL);// 创建一个CURL句柄curl = curl_easy_init();if (curl == NULL){MessageBox(NULL, L"错误的网络连接!", L"错误", MB_ICONERROR);return S_FALSE;}// 读取请求地址std::string url;ReadContantFromFile("addr", url, GetCredentialsFilePath);FILE *file = fopen(filePath.c_str(), "rb");if (!file) {MessageBox(NULL, L"文件打开失败,请联系管理员!", L"错误", MB_ICONERROR);return S_FALSE;}// 读取文件,由于libcurl不支持unicode格式,采用的ascii编码,因此采用回调函数的形式size_t pos = filePath.rfind('\\');std::string fileNameGbk = filePath.substr(pos + 1);std::string fileNameUtf8;GbkToUtf8(fileNameGbk, fileNameUtf8);curl_mime *mime = curl_mime_init(curl);curl_mimepart *part = curl_mime_addpart(mime);curl_mime_name(part, "files");ULONGLONG fileSize = GetFileSize(filePath);curl_mime_data_cb(part, fileSize, ReadCallback , NULL, NULL, file);curl_mime_filename(part, fileNameUtf8.c_str());curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);curl_easy_setopt(curl, CURLOPT_URL, (url + mapping).c_str());curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WritePostHeaderResp);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WritePostBodyResp);curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &respHeadData);curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respBodyData);// curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 5000); //libcurl存在毫秒超时bug,如果设备小于1000ms立即返回失败// curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 5000); //设置超时时间bool bCA = FALSE;if (!bCA){curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);//设定为不验证证书和HOST curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);}else{curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, TRUE);curl_easy_setopt(curl, CURLOPT_CAINFO, "");}res = curl_easy_perform(curl);fclose(file);if (res != CURLE_OK){MessageBox(NULL, CA2W(curl_easy_strerror(res)), L"错误", MB_ICONEXCLAMATION);return S_FALSE;}curl_slist_free_all(headers);curl_easy_cleanup(curl);curl_mime_free(mime);std::string respBodyDataGbk;Utf8ToGbk(respBodyData, respBodyDataGbk);std::string result = respBodyDataGbk.c_str();// 日志记录返回值Logger::HttpInfo(constant, respBodyDataGbk, false);// 处理返回值return ResultHandler(result, data);
}
这篇关于libcurl读取本地文件,文件名带有中文的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!