Skip to content

Instantly share code, notes, and snippets.

@henkman
Last active December 15, 2023 09:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henkman/2e7a4dcf4822bc0029d7d2af731da5c5 to your computer and use it in GitHub Desktop.
Save henkman/2e7a4dcf4822bc0029d7d2af731da5c5 to your computer and use it in GitHub Desktop.
https request in winhttp
#include <stdio.h>
#include <windows.h>
#include <winhttp.h>
static int http_put(wchar_t *host, short port, wchar_t *path, char *data, size_t size)
{
int code = 0;
HINTERNET hSession = WinHttpOpen(NULL,
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
code = 1;
goto end;
}
HINTERNET hConnect = WinHttpConnect(hSession, host, port, 0);
if (!hConnect)
{
code = 2;
goto end;
}
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"PUT", L"/",
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
if (!hRequest)
{
code = 3;
goto end;
}
DWORD dwFlags =
SECURITY_FLAG_IGNORE_UNKNOWN_CA |
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
if (!WinHttpSetOption(
hRequest,
WINHTTP_OPTION_SECURITY_FLAGS,
&dwFlags,
sizeof(dwFlags)))
{
code = 4;
goto end;
}
if (!WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
size, 0))
{
code = 5;
goto end;
}
DWORD dwBytesWritten;
if (!WinHttpWriteData(hRequest, data, size, &dwBytesWritten))
{
code = 6;
goto end;
}
end:
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return code;
}
int main(void)
{
char *popit = "yo dawg";
int code = http_put(L"localhost", 8080, L"/", popit, sizeof(popit) - 1);
if (code)
{
printf("error: %d\n", code);
}
return 0;
}
#include <stdio.h>
#include <windows.h>
#include <winhttp.h>
int main(void)
{
BOOL bResults = FALSE;
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
printf("go\n");
hSession = WinHttpOpen(L"abc",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
printf("hSession %08x\n", hSession);
if (hSession)
hConnect = WinHttpConnect(hSession, L"localhost",
INTERNET_DEFAULT_HTTPS_PORT, 0);
printf("hConnect %08x\n", hConnect);
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/a.c",
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
printf("hRequest %08x\n", hRequest);
if (hRequest)
{
DWORD dwFlags =
SECURITY_FLAG_IGNORE_UNKNOWN_CA |
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
WinHttpSetOption(
hRequest,
WINHTTP_OPTION_SECURITY_FLAGS,
&dwFlags,
sizeof(dwFlags));
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0, WINHTTP_NO_REQUEST_DATA, 0,
0, 0);
}
if (bResults)
{
if (WinHttpReceiveResponse(hRequest, NULL))
{
DWORD dwDownloaded, dwSize;
do
{
if (!WinHttpQueryDataAvailable(hRequest, &dwSize) && dwSize)
break;
char *buf = malloc(dwSize);
if (!buf)
break;
WinHttpReadData(hRequest, (LPVOID)buf, dwSize, &dwDownloaded);
fwrite(buf, sizeof(char), dwSize, stdout);
free(buf);
if (!dwDownloaded)
break;
} while (dwSize > 0);
}
}
else
printf("Error %d has occurred.\n", GetLastError());
if (hRequest)
WinHttpCloseHandle(hRequest);
if (hConnect)
WinHttpCloseHandle(hConnect);
if (hSession)
WinHttpCloseHandle(hSession);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment