如何解决通过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,0);
HINTERNET hConnect = InternetConnect(hSession,"127.0.0.1",INTERNET_DEFAULT_HTTP_PORT,INTERNET_SERVICE_HTTP,1);
HINTERNET hRequest = HttpOpenRequestA(hConnect,"POST","/test/index.PHP",1);
HttpSendRequestA(hRequest,hdrs,strlen(hdrs),frmdata,strlen(frmdata));
}
我也尝试使用static CHAR frmdata[] = "data=" + name;
,但没有用。
这是我收到的错误:
错误:类型'const char [6]'和'TCHAR [257] {aka char [257]}'的无效操作数对二进制'operator +'|
解决方法
您无法像尝试那样连接char[]
数组。分配一个足够大的单独缓冲区,然后将输入字符串复制到其中,例如:
#include <windows.h>
#include <Lmcons.h>
#include <WinInet.h>
#include <cstring>
//#include <cstdio>
using namespace std;
#pragma comment( lib,"Wininet.lib")
int main()
{
static char hdrs[] = "Content-Type: application/x-www-form-urlencoded";
char name[UNLEN + 1] = {};
DWORD size = UNLEN + 1;
GetUserNameA(name,&size);
char frmdata[5 + UNLEN + 1] = {};
strcpy(frmdata,"data=");
strcat(frmdata,name);
// alternatively:
// sprintf(frmdata,"data=%s",name);
HINTERNET hSession = InternetOpenA("http generic",INTERNET_OPEN_TYPE_PRECONFIG,NULL,0);
if (!hSession) {
// error handling...
}
HINTERNET hConnect = InternetConnectA(hSession,"127.0.0.1",INTERNET_DEFAULT_HTTP_PORT,INTERNET_SERVICE_HTTP,1);
if (!hConnect) {
// error handling...
}
HINTERNET hRequest = HttpOpenRequestA(hConnect,"POST","/test/index.php",1);
if (!hRequest) {
// error handling...
}
if (!HttpSendRequestA(hRequest,hdrs,strlen(hdrs),frmdata,strlen(frmdata))) {
// error handling...
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return 0;
}
或者,您可以使用单个缓冲区,只要增大缓冲区的大小,然后GetUserNameA()
填充其中的一部分,然后就不需要再进行复制了,例如:
int main()
{
...
char frmdata[5 + UNLEN + 1] = "data=";
DWORD size = UNLEN + 1;
GetUserNameA(&frmdata[5],&size);
...
}
,
您需要连接,std :: string可以为您完成。您还应该使用相同的基本字符类型,以避免进行其他转换。
std::wstring
如果要传输unicode数据(在这种情况下,请使用WideCharToMultiByte
),则需要将数据字符串转换为正确的utf-8(查找{
"name": "ts-logger","main": "dist/index.js","types": "dist/index.d.ts","scripts": {
"install": "tsc"
},"repository": {
"type": "git","url": "https://github.com/vkrenta/ts-logger.git"
},"devDependencies": {
"typescript": "^4.0.2","@types/node": "^14.6.4"
},"dependencies": {}
...
}
)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。