View difference between Paste ID: VLETpGHq and n0yYwGwp
SHOW: | | - or go back to the newest paste.
1
#include <windows.h>
2
#include <wininet.h>
3
4
#include <iostream>
5
6
#pragma comment(lib, "wininet.lib")
7
8
int main ()
9
{
10
    std::cout << "About to send request..." << std::endl;
11
12
    HINTERNET hIntSession = InternetOpenW (L"IoTTest", INTERNET_OPEN_TYPE_PRECONFIG, nullptr, nullptr, 0);
13
    if (hIntSession == nullptr) {
14
        std::cout << "InternetOpenW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
15
16
        return -1;
17
    }
18
19
    HINTERNET hHttpSession = InternetConnectW (hIntSession, L"127.0.0.1", 8080, nullptr, nullptr, INTERNET_SERVICE_HTTP, 0, 0);
20
    if (hHttpSession == nullptr) {
21
        std::cout << "InternetConnectW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
22
23
        return -1;
24
    }
25
26
    // Example command (note the Base64 padding at the end, URL-encoded): ZGV2Y29uIGRwX2FkZCBjOlxkYXRhXHVzZXJzXGRlZmF1bHRhY2NvdW50XG5ldDc4MDAtYXJtLW42NTBmLmluZg%3D%3D
27
    HINTERNET hHttpRequest = HttpOpenRequestW (hHttpSession, L"POST", L"/api/iot/processmanagement/runcommand?command=[YOUR COMMAND GOES HERE (base64)]&runasdefaultaccount=ZmFsc2U%3D", nullptr, nullptr, nullptr, 0, 0);
28
    if (hHttpRequest == nullptr) {
29
        std::cout << "HttpOpenRequestW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
30
31
        return -1;
32
    }
33
34-
    // Example (note the Base64 padding at the end, URL-encoded): QWRtaW5pc3RyYXRvcjpwYXNzd29yZA==
34+
    // Example (note the Base64 padding at the end, NOT URL-encoded): QWRtaW5pc3RyYXRvcjpwYXNzd29yZA==
35
    WCHAR* pHeaders = L"Accept-Encoding: gzip, deflate\nAuthorization: Basic [BASE64 encoded auth info in the following format: user:password]";
36
    if (HttpSendRequestW (hHttpRequest, pHeaders, (DWORD)wcslen (pHeaders), nullptr, 0) == FALSE) {
37
        std::cout << "HttpSendRequestW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
38
39
        return -1;
40
    }
41
42
    DWORD status = 0;
43
    DWORD size = sizeof status;
44
    if (HttpQueryInfoW (hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, nullptr) == FALSE) {
45
        std::cout << "HttpQueryInfoW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
46
47
        return -1;
48
    }
49
50
    std::cout << "HTTP status: " << status << std::endl;
51
52
    CHAR someData[256] = {};
53
    DWORD bytesRead;
54
    if (InternetReadFile (hHttpRequest, someData, sizeof someData - 1 /*keep it null terminated*/, &bytesRead) != FALSE) {
55
        std::cout << "First few bytes of response: " << someData << std::endl;
56
    }
57
58
    ::InternetCloseHandle (hHttpRequest);
59
    ::InternetCloseHandle (hHttpSession);
60
    ::InternetCloseHandle (hIntSession);
61
}