我正在制作一个 C++ 代理,它将使用 HttpSendRequest() 将信息(例如系统主机名)发送回中央服务器。我希望它发回的信息之一是操作系统。我创建了以下函数来获取系统主机名。
wstring getOS()
{
HKEY key;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_QUERY_VALUE, &key); // Obtains Registry handle
DWORD type;
wchar_t buffer[MAX_PATH]; // MAX_PATH = 260 - The system hostname should never exceed this value
DWORD size = sizeof(buffer);
RegQueryValueEx(key, L"ProductName", NULL, &type, (LPBYTE)&buffer, &size); // Queries Registry key - stores value in "buffer"
wstring os(buffer); // Converts from C-style character array to wstring
return os; // Returns wstring to caller
}
Run Code Online (Sandbox Code Playgroud)
该函数将使用注册表获取操作系统并将其存储为 wstring。然后,我想将返回的“os”wstring 传递给以下 post() 函数,但我注意到您必须使用字符串而不是 HTTP POST 数据的 wstring。下面是我的 post() 函数的代码:
void post()
{
HINTERNET hInternetOpen = InternetOpen(userAgent.c_str(), INTERNET_OPEN_TYPE_PROXY, L"http://127.0.0.1:9999", NULL, 0);
HINTERNET hInternetConnect = InternetConnect(hInternetOpen, host.c_str(), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
HINTERNET hHttpOpenRequest = HttpOpenRequest(hInternetConnect, L"POST", file.c_str(), NULL, NULL, NULL, 0, 0);
wstring headers = L"Content-Type: application/x-www-form-urlencoded"; // Content-Type is necessary to POST
string postData = "os="; // Why does this have to be a string and not a wstring?
HttpSendRequest(hHttpOpenRequest, headers.c_str(), headers.length(), (LPVOID)postData.c_str(), postData.size());
InternetCloseHandle(hInternetOpen);
InternetCloseHandle(hInternetConnect);
InternetCloseHandle(hHttpOpenRequest);
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试将“postData”设为 wstring,我会得到如下图所示的内容:
有人可以阐明包含 wstring 作为 POST 数据的最简单方法吗?
HttpSendRequest()只知道原始字节,而不知道字符串。您可以使用 发送 UTF-16 数据,但您必须通过标头中的属性std::wstring告诉服务器您正在发送 UTF-16 。charsetContent-Type
wstring headers = L"Content-Type: application/x-www-form-urlencoded; charset=utf-16";
// TODO: don't forget to URL-encode the value from getOS() to
// escape reserved characters, including '=' and '&'...
wstring postData = L"os=" + getOS();
HttpSendRequest(hHttpOpenRequest, headers.c_str(), headers.length(),
postData.c_str(), postData.length() * sizeof(wchar_t));
Run Code Online (Sandbox Code Playgroud)
注意上面的使用sizeof(wchar_t)。在你的屏幕截图中,你的嗅探器显示了原始数据,它显示的数据是 UTF-16 的样子,但你只看到一半的wstring数据,因为你将的dwOptionalLength参数设置HttpSendRequest()为字符数(7) 而不是字节数(14):
dwOptionalLength [in]
可选数据的大小,以字节为单位。如果没有可选数据要发送,则该参数可以为零。
当您使用 时std::string,字符数和字节数是相同的值。
您真正应该发送的是 UTF-8 而不是 UTF-16,例如:
string Utf8Encode(const wstring &wstr)
{
// NOTE: C++11 has built-in support for converting between
// UTF-8 and UTF-16. See the std::wstring_convert class...
/*
wstring_convert<codecvt_utf8_utf16<wchar_t>> conv;
return conv.to_bytes(wstr);
*/
string out;
int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL);
if (len > 0)
{
out.resize(len);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), &out[0], len, NULL, NULL);
}
return out;
}
Run Code Online (Sandbox Code Playgroud)
wstring headers = L"Content-Type: application/x-www-form-urlencoded; charset=utf-8";
// TODO: don't forget to URL-encode the value from getOS() to
// escape reserved characters, including '=' and '&'...
string postData = "os=" + Utf8Encode(getOS());
HttpSendRequest(hHttpOpenRequest, headers.c_str(), headers.length(),
postData.c_str(), postData.size());
Run Code Online (Sandbox Code Playgroud)