HttpSendRequest 截断我的值,而 HttpSendRequest 按预期工作
HttpSendRequest trucate my value while HttpSendRequestA works as excepted
我想使用 HttpSendRequest
在 C++ 中发送一个 POST 值。我在 https://requestbin.net 上测试请求
我制作了这段代码:
void sendValue()
{
LPCSTR header = "Content-Type: application/x-www-form-urlencoded; charset=utf-8";
std::string dataStr = u8"result=a very longue value !!!!!!!!!!!!!!!!!!!!!!!!!!!! :)";
LPVOID myMessage = (LPVOID)dataStr.c_str();
HINTERNET hInternet = InternetOpenA("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnection = InternetConnectA(hInternet, "requestbin.net", 80, " ", " ", INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequestA(hConnection, "POST", "/r/IDREQUESTBIN", NULL, NULL, NULL, 0, 1);
HttpSendRequestA(hRequest, header, strlen(header), myMessage, dataStr.size());
std::cout << "wcslen(header) " << strlen(header) << std::endl;
std::cout << "dataStr.size() " << dataStr.size() << std::endl;
}
完美运行。我现在想使用 wstring
,我编写了这段代码:
void sendValueV2()
{
LPCWSTR header = L"Content-Type: application/x-www-form-urlencoded; charset=utf-8";
std::wstring dataStr = L"result=a very longue value !!!!!!!!!!!!!!!!!!!!!!!!!!!! :)";
LPVOID myMessage = (LPVOID)dataStr.c_str();
HINTERNET hInternet = InternetOpen(L"InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnection = InternetConnect(hInternet, L"requestbin.net", 80, L" ", L" ", INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnection, L"POST", L"/r/IDREQUESTBIN", NULL, NULL, NULL, 0, 1);
HttpSendRequest(hRequest, header, wcslen(header), myMessage, dataStr.size());
std::wcout << "wcslen(header) " << wcslen(header) << std::endl;
std::wcout << "dataStr.size() " << dataStr.size() << std::endl;
}
我的 POST 值被截断为:result=a very longue value !!
这是我的控制台的输出:
wcslen(header) 62
dataStr.size() 58
wcslen(header) 62
dataStr.size() 58
有人可以向我解释发生了什么事吗?我要疯了。
根据documentation,HttpSendRequest
的dwOptional
参数是以字节表示的,不是字符,所以替换为:
HttpSendRequest(hRequest, header, wcslen(header), myMessage, dataStr.size());
与:
HttpSendRequest(hRequest, header, wcslen(header), myMessage, dataStr.size() * sizeof (WCHAR));
你的问题应该已经解决了。
我想使用 HttpSendRequest
在 C++ 中发送一个 POST 值。我在 https://requestbin.net 上测试请求
我制作了这段代码:
void sendValue()
{
LPCSTR header = "Content-Type: application/x-www-form-urlencoded; charset=utf-8";
std::string dataStr = u8"result=a very longue value !!!!!!!!!!!!!!!!!!!!!!!!!!!! :)";
LPVOID myMessage = (LPVOID)dataStr.c_str();
HINTERNET hInternet = InternetOpenA("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnection = InternetConnectA(hInternet, "requestbin.net", 80, " ", " ", INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequestA(hConnection, "POST", "/r/IDREQUESTBIN", NULL, NULL, NULL, 0, 1);
HttpSendRequestA(hRequest, header, strlen(header), myMessage, dataStr.size());
std::cout << "wcslen(header) " << strlen(header) << std::endl;
std::cout << "dataStr.size() " << dataStr.size() << std::endl;
}
完美运行。我现在想使用 wstring
,我编写了这段代码:
void sendValueV2()
{
LPCWSTR header = L"Content-Type: application/x-www-form-urlencoded; charset=utf-8";
std::wstring dataStr = L"result=a very longue value !!!!!!!!!!!!!!!!!!!!!!!!!!!! :)";
LPVOID myMessage = (LPVOID)dataStr.c_str();
HINTERNET hInternet = InternetOpen(L"InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnection = InternetConnect(hInternet, L"requestbin.net", 80, L" ", L" ", INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnection, L"POST", L"/r/IDREQUESTBIN", NULL, NULL, NULL, 0, 1);
HttpSendRequest(hRequest, header, wcslen(header), myMessage, dataStr.size());
std::wcout << "wcslen(header) " << wcslen(header) << std::endl;
std::wcout << "dataStr.size() " << dataStr.size() << std::endl;
}
我的 POST 值被截断为:result=a very longue value !!
这是我的控制台的输出:
wcslen(header) 62
dataStr.size() 58
wcslen(header) 62
dataStr.size() 58
有人可以向我解释发生了什么事吗?我要疯了。
根据documentation,HttpSendRequest
的dwOptional
参数是以字节表示的,不是字符,所以替换为:
HttpSendRequest(hRequest, header, wcslen(header), myMessage, dataStr.size());
与:
HttpSendRequest(hRequest, header, wcslen(header), myMessage, dataStr.size() * sizeof (WCHAR));
你的问题应该已经解决了。