使用HttpOpenRequestA发送POST数据时字符串拼接报错求助
使用HttpOpenRequestA发送POST数据时拼接用户名失败的解决方案
先来看你遇到的问题场景和错误:
我尝试将本机名称发送到本地服务器并保存到文件中,使用HttpOpenRequestA发送POST数据,以下是我的代码:
#include <stdio.h> #include <windows.h> #include <iostream> #include <fstream> #include <string> #include <string.h> #include <cstring> #include <Lmcons.h> #include <unistd.h> #include <stdlib.h> #include <WinInet.h> #include <bits/stdc++.h> #pragma comment( lib,"Wininet.lib") using namespace std; int main() { TCHAR name [ UNLEN + 1 ]; DWORD size = UNLEN + 1; static CHAR hdrs[] = "Content-Type: application/x-www-form-urlencoded"; if (GetUserName( (TCHAR*)name, &size )); static CHAR frmdata[] = "data=", name; HINTERNET hSession = InternetOpenA("http generic", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); HINTERNET hConnect = InternetConnect(hSession, "127.0.0.1", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); HINTERNET hRequest = HttpOpenRequestA(hConnect, "POST", "/test/index.php", NULL, NULL, NULL, 0, 1); HttpSendRequestA(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata)); }我也曾尝试使用
static CHAR frmdata[] = "data=" + name;的写法,但仍然无效,收到的错误信息为:
error: invalid operands of types 'const char [6]' and 'TCHAR [257] {aka char [257]}' to binary 'operator+'|
嘿,这个问题其实很好解决!核心问题在于你误解了C++里字符串数组的拼接方式,还有一些细节没处理好,我给你一步步梳理:
错误原因
- 字符串数组不能直接用
+拼接:C++里的字符数组(char[])不是std::string,没法直接用+运算符把两个数组拼起来,这就是你看到那个编译错误的原因。 - GetUserName的调用有问题:你写的
if (GetUserName(...));后面多了个分号,这意味着不管调用成功失败,都不会执行任何后续处理,万一获取用户名失败你也不知道。 - WinINet句柄没清理:打开的
hSession、hConnect、hRequest用完后没关闭,会造成资源泄漏。
修正后的代码
我把这些问题都修复了,你可以直接用:
#include <stdio.h> #include <windows.h> #include <string> #include <WinInet.h> #pragma comment(lib, "Wininet.lib") using namespace std; int main() { TCHAR name[UNLEN + 1]; DWORD size = UNLEN + 1; // 检查用户名获取是否成功 if (!GetUserName(name, &size)) { printf("Failed to get username! Error code: %d\n", GetLastError()); return 1; } // 使用std::string方便拼接POST数据 string username(name); string frmdata = "data=" + username; const char* hdrs = "Content-Type: application/x-www-form-urlencoded"; // 初始化WinINet会话 HINTERNET hSession = InternetOpenA("HTTP Generic Client", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (!hSession) { printf("InternetOpen failed! Error code: %d\n", GetLastError()); return 1; } // 连接到本地服务器 HINTERNET hConnect = InternetConnectA(hSession, "127.0.0.1", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if (!hConnect) { printf("InternetConnect failed! Error code: %d\n", GetLastError()); InternetCloseHandle(hSession); return 1; } // 创建POST请求 HINTERNET hRequest = HttpOpenRequestA(hConnect, "POST", "/test/index.php", NULL, NULL, NULL, 0, 0); if (!hRequest) { printf("HttpOpenRequest failed! Error code: %d\n", GetLastError()); InternetCloseHandle(hConnect); InternetCloseHandle(hSession); return 1; } // 发送POST数据 if (!HttpSendRequestA(hRequest, hdrs, strlen(hdrs), frmdata.c_str(), frmdata.size())) { printf("HttpSendRequest failed! Error code: %d\n", GetLastError()); } // 依次关闭所有WinINet句柄,避免资源泄漏 if (hRequest) InternetCloseHandle(hRequest); if (hConnect) InternetCloseHandle(hConnect); if (hSession) InternetCloseHandle(hSession); return 0; }
关键修改点说明
- 用
std::string来拼接POST数据:std::string支持+运算符,轻松把"data="和用户名拼在一起,还能自动管理内存。 - 增加了错误检查:每个WinINet函数调用都加了失败判断,方便你排查问题。
- 清理了WinINet句柄:按照创建的反向顺序关闭所有句柄,避免资源泄漏。
- 去掉了不必要的头文件:比如
unistd.h、bits/stdc++.h这些在Windows下没必要的头文件,让代码更简洁。
内容的提问来源于stack exchange,提问作者Vivo Madrid
相关产品推荐
相关产品推荐

