Problems encountered when downloading files with objectarx + libcurl

  1. The download failed, causing CAD to crash, with the error 'Error handler re-entered.Exiting now '. The reason is because I inherited libcurl-related functions into a class and performed related webapi interactions in the class, but because of the initial The request was made, so curl was not initialized, resulting in a data transmission error. It only needs to be initialized at the beginning of the function.
curl = curl_easy_init();
/*curl_global_init(CURL_GLOBAL_DEFAULT);*/
if (curl)
{
	MessageBoxA(NULL, combinePath.c_str(), "0", 0);
	FILE* fp = fopen(strFileName.c_str(), "wb");    // 打开文件,准备写入
	curl_easy_setopt(curl, CURLOPT_URL, combinePath.c_str());
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunction);
	
	CURLcode result = curl_easy_perform(curl);
	acutPrintf(L"download :  %i", result);
	fclose(fp);                         // 关闭文件
	/*curl_easy_cleanup(curl);*/
}
  1. Requesting webapi data, I found that the interface was unresponsive, and testing the incoming string alone had no effect. The reason was that my test file was in English characters, which caused me to ignore the encoding and passed it in, and I had always ignored this aspect in testing. Therefore, to solve this problem, we need to encode the Chinese characters and then pass them into the header.
std::wstring_convert<codecvt_utf8<wchar_t>> converter;

//std::wstring wideStr = L"专篇模板";
std::wstring wideStr = ConvertToWideString(Filepath);
// 转换为 UTF-8 编码
std::string utf8Str = converter.to_bytes(wideStr);

// 进行 URL 编码
char* encodedData = curl_easy_escape(curl, utf8Str.c_str(), utf8Str.length());
std::string encodedSymbolName(encodedData);
curl_free(encodedData);

urlencode function

std::string webApi::UrlEncode(const std::string& str)
{
	std::string strTemp = "";
	size_t length = str.length();
	for (size_t i = 0; i < length; i++)
	{
		if (isalnum((unsigned char)str[i]) ||
			(str[i] == '-') ||
			(str[i] == '_') ||
			(str[i] == '.') ||
			(str[i] == '~'))
			strTemp += str[i];
		else if (str[i] == ' ')
			strTemp += "+";
		else
		{
			strTemp += '%';
			strTemp += ToHex((unsigned char)str[i] >> 4);
			strTemp += ToHex((unsigned char)str[i] % 16);
		}
	}
	return strTemp;

}

Guess you like

Origin blog.csdn.net/qq_41059339/article/details/134562024