Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <wininet.h>
- #include <iostream>
- #pragma comment(lib, "wininet.lib")
- int main ()
- {
- std::cout << "About to send request..." << std::endl;
- HINTERNET hIntSession = InternetOpenW (L"IoTTest", INTERNET_OPEN_TYPE_PRECONFIG, nullptr, nullptr, 0);
- if (hIntSession == nullptr) {
- std::cout << "InternetOpenW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
- return -1;
- }
- HINTERNET hHttpSession = InternetConnectW (hIntSession, L"127.0.0.1", 8080, nullptr, nullptr, INTERNET_SERVICE_HTTP, 0, 0);
- if (hHttpSession == nullptr) {
- std::cout << "InternetConnectW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
- return -1;
- }
- // Example command (note the Base64 padding at the end, URL-encoded): ZGV2Y29uIGRwX2FkZCBjOlxkYXRhXHVzZXJzXGRlZmF1bHRhY2NvdW50XG5ldDc4MDAtYXJtLW42NTBmLmluZg%3D%3D
- 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);
- if (hHttpRequest == nullptr) {
- std::cout << "HttpOpenRequestW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
- return -1;
- }
- // Example (note the Base64 padding at the end, NOT URL-encoded): QWRtaW5pc3RyYXRvcjpwYXNzd29yZA==
- WCHAR* pHeaders = L"Accept-Encoding: gzip, deflate\nAuthorization: Basic [BASE64 encoded auth info in the following format: user:password]";
- if (HttpSendRequestW (hHttpRequest, pHeaders, (DWORD)wcslen (pHeaders), nullptr, 0) == FALSE) {
- std::cout << "HttpSendRequestW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
- return -1;
- }
- DWORD status = 0;
- DWORD size = sizeof status;
- if (HttpQueryInfoW (hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, nullptr) == FALSE) {
- std::cout << "HttpQueryInfoW failed! Lasterror:" << std::hex << GetLastError () << std::endl;
- return -1;
- }
- std::cout << "HTTP status: " << status << std::endl;
- CHAR someData[256] = {};
- DWORD bytesRead;
- if (InternetReadFile (hHttpRequest, someData, sizeof someData - 1 /*keep it null terminated*/, &bytesRead) != FALSE) {
- std::cout << "First few bytes of response: " << someData << std::endl;
- }
- ::InternetCloseHandle (hHttpRequest);
- ::InternetCloseHandle (hHttpSession);
- ::InternetCloseHandle (hIntSession);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement