mfc
1.让str支持任意编码的中文字符,您可以使用urlencode函数对其进行编码。这样,无论str中的字符是什么编码,它们都将被正确地传递给URL。下面是如何在MFC中实现urlencode的示例:
CString strl = NULL;
GetDlgItemText(IDC_EDIT1, str);
CString UrlEncode(const CString& str)
{
CStringA utf8Str;
WideCharToMultiByte(CP_UTF8, 0, str.GetString(), -1, utf8Str.GetBuffer(str.GetLength() * 4), str.GetLength() * 4, NULL, NULL);
utf8Str.ReleaseBuffer();
CString encodedStr;
for (int i = 0; i < utf8Str.GetLength(); ++i)
{
char ch = utf8Str[i];
if (isalnum((unsigned char)ch) || ch == '-' || ch == '_' || ch == '.' || ch == '~')
{
encodedStr += ch;
}
else
{
encodedStr.AppendFormat(_T("%%%02X"), (unsigned char)ch);
}
}
return encodedStr;
}
使用方法
CString encodedStr = UrlEncode(str);
pHttpFile = (CHttpFile*)sess.OpenURL(_T("https://sadasahz.top/asad.php?key=daf&shuo=") + encodedStr);
2将中文字符转换为HTML实体
CString ConvertToHtmlEntities(const CString& input)
{
CString output;
for (int i = 0; i < input.GetLength(); ++i)
{
wchar_t ch = input[i];
if (ch > 127)
{
CString entity;
entity.Format(L"&#x%X;", static_cast<unsigned int>(ch));
output += entity;
}
else
{
output += ch;
}
}
return output;
}
使用方法
CString htmlEntitiesContent = ConvertToHtmlEntities(content);
std::string command = "mail.exe -u \"" + CStringToStdString(recipient) + "\" -i \"" + sender + "\" -p \"" + password + "\" -s \"Automail\" -b \"" + CStringToStdString(htmlEntitiesContent) + "\"";
3将编码转化为Unicode编码发送到Edit contorl控件 // Convert the content to Unicode
int unicodeLength = MultiByteToWideChar(CP_UTF8, 0, receivedBytes.GetString(), -1, NULL, 0);
wchar_t* unicodeContent = new wchar_t[unicodeLength];
MultiByteToWideChar(CP_UTF8, 0, receivedBytes.GetString(), -1, unicodeContent, unicodeLength);
strl = unicodeContent;
delete[] unicodeContent;
使用方法:
int unicodeLength = MultiByteToWideChar(CP_UTF8, 0, receivedBytes.GetString(), -1, NULL, 0);
wchar_t* unicodeContent = new wchar_t[unicodeLength];
MultiByteToWideChar(CP_UTF8, 0, receivedBytes.GetString(), -1, unicodeContent, unicodeLength);
strl = unicodeContent;
delete[] unicodeContent;
SetDlgItemText(IDC_EDIT2, strl);
4将Cstring转为操作cmd可以执行的类型
std::string CStringToStdString(const CString& cstr)
{
CT2CA pszConvertedAnsiString(cstr);
std::string strStd(pszConvertedAnsiString);
return strStd;
}
使用 std::string command = "mail.exe -u \"" + CStringToStdString(recipient) + "\" -i \"" + sender + "\" -p \"" + password + "\" -s \"Automail\" -b \"" + CStringToStdString(htmlEntitiesContent) + "\"";