通过HttpOpenRequestA发送POST数据

时间:2020-09-09 11:45:01

标签: c++ sockets winapi wininet

我正在尝试将PC名称发送到本地服务器并将其保存在文件中。

有我的代码:

#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;,但没有用。 这是我收到的错误:

错误:类型'const char [6]'和'TCHAR [257] {aka char [257]}'的无效操作数对二进制'operator +'|

2 个答案:

答案 0 :(得分:1)

您无法像尝试那样连接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, NULL, 0);
    if (!hSession) {
        // error handling...
    }

    HINTERNET hConnect = InternetConnectA(hSession, "127.0.0.1", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
    if (!hConnect) {
        // error handling...
    }

    HINTERNET hRequest = HttpOpenRequestA(hConnect, "POST", "/test/index.php", NULL, NULL, NULL, 0, 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);

    ...
}

答案 1 :(得分:0)

您需要连接,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": {} ... } )。

相关问题