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;

	// 设置Token
	if (isVerify)
	{
		// 读取Token
		std::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());
	}

	// 初始化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_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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值