gpt4 book ai didi

c++ - HttpSendRequest - 不支持 Unicode 的 POST 数据

转载 作者:行者123 更新时间:2023-11-28 00:04:01 25 4
gpt4 key购买 nike

我正在制作一个 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
}

此函数将使用注册表获取操作系统并将其存储为 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);
}

如果我尝试将“postData”设为 wstring,我会得到如下图所示的内容:

HTTP POST data as wstring

有人可以阐明将 wstring 作为 POST 数据的最简单方法吗?

最佳答案

HttpSendRequest()只知道原始字节,不知道字符串。您可以使用 std::wstring 发送 UTF-16 数据, 但你必须通过 charset 告诉服务器你正在发送 UTF-16 Content-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));

注意 sizeof(wchar_t) 的使用多于。在您的屏幕截图中,您的嗅探器正在显示原始 数据,它显示的数据是 UTF-16 的样子,但您只看到 wstring 的一半。数据,因为您正在设置 dwOptionalLength HttpSendRequest() 的参数字符数 (7) 而不是字节数 (14):

dwOptionalLength [in]
The size of the optional data, in bytes. This parameter can be zero if there is no optional data to send.

当您使用 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;
}

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());

关于c++ - HttpSendRequest - 不支持 Unicode 的 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36774547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com