Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send POST data via HttpOpenRequestA

I'm trying to send my PC Name to my local server and save it in a file.

There is my code:

#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));
}

I've tried use static CHAR frmdata[] = "data=" + name; also but not works. This is the error that I receive:

error: invalid operands of types 'const char [6]' and 'TCHAR [257] {aka char [257]}' to binary 'operator+'|

like image 420
Vivo Madrid Avatar asked Jul 10 '26 02:07

Vivo Madrid


1 Answers

You can't concatenate char[] arrays like you are trying to do. Allocate a separate buffer of sufficient size and copy the input strings into it, eg:

#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;
}

Alternatively, you can use a single buffer, just make it larger, and have GetUserNameA() populate a portion of it, then you don't need to make a copy afterwards, eg:

int main()
{
    ...

    char frmdata[5 + UNLEN + 1] = "data=";
    DWORD size = UNLEN + 1;
    GetUserNameA(&frmdata[5], &size);

    ...
}
like image 147
Remy Lebeau Avatar answered Jul 14 '26 16:07

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!