Skip to content

Instantly share code, notes, and snippets.

@AhnMo
Last active March 15, 2024 09:50
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save AhnMo/5cf37bbd9a6fa2567f99ac0528eaa185 to your computer and use it in GitHub Desktop.
Save AhnMo/5cf37bbd9a6fa2567f99ac0528eaa185 to your computer and use it in GitHub Desktop.
Wininet HTTP Client Example
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#pragma comment (lib, "Wininet.lib")
int main(int argc, char *argv[]) {
HINTERNET hSession = InternetOpen(
L"Mozilla/5.0", // User-Agent
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0);
HINTERNET hConnect = InternetConnect(
hSession,
L"www.google.com", // HOST
0,
L"",
L"",
INTERNET_SERVICE_HTTP,
0,
0);
HINTERNET hHttpFile = HttpOpenRequest(
hConnect,
L"GET", // METHOD
L"/", // URI
NULL,
NULL,
NULL,
0,
0);
while (!HttpSendRequest(hHttpFile, NULL, 0, 0, 0)) {
printf("HttpSendRequest error : (%lu)\n", GetLastError());
InternetErrorDlg(
GetDesktopWindow(),
hHttpFile,
ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA |
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
NULL);
}
/*
char bufQuery[32];
DWORD dwLengthBufQuery;
dwLengthBufQuery = sizeof(bufQuery);
DWORD dwIndex;
dwIndex = 0;
// get Content-Length value but... too small
BOOL bQuery;
bQuery = HttpQueryInfo(
hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
bufQuery,
&dwLengthBufQuery,
&dwIndex);
if (!bQuery)
printf("HttpQueryInfo error : <%lu>\n", GetLastError());
*/
DWORD dwFileSize;
//dwFileSize = (DWORD)atol(bufQuery);
dwFileSize = BUFSIZ;
char* buffer;
buffer = new char[dwFileSize + 1];
while (true) {
DWORD dwBytesRead;
BOOL bRead;
bRead = InternetReadFile(
hHttpFile,
buffer,
dwFileSize + 1,
&dwBytesRead);
if (dwBytesRead == 0) break;
if (!bRead) {
printf("InternetReadFile error : <%lu>\n", GetLastError());
} else {
buffer[dwBytesRead] = 0;
printf("Retrieved %lu data bytes: %s\n", dwBytesRead, buffer);
}
}
InternetCloseHandle(hHttpFile);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return 0;
}
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#pragma comment (lib, "Wininet.lib")
int main(int argc, char *argv[]) {
HINTERNET hSession = InternetOpen(
L"Mozilla/5.0",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0);
HINTERNET hConnect = InternetConnect(
hSession,
L"www.google.com",
INTERNET_DEFAULT_HTTPS_PORT, // THIS
L"",
L"",
INTERNET_SERVICE_HTTP,
0,
0);
HINTERNET hHttpFile = HttpOpenRequest(
hConnect,
L"GET",
L"/",
NULL,
NULL,
NULL,
INTERNET_FLAG_SECURE, // THIS
0);
while (!HttpSendRequest(hHttpFile, NULL, 0, 0, 0)) {
printf("HttpSendRequest error : (%lu)\n", GetLastError());
InternetErrorDlg(
GetDesktopWindow(),
hHttpFile,
ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA |
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
NULL);
}
DWORD dwFileSize;
dwFileSize = BUFSIZ;
char* buffer;
buffer = new char[dwFileSize + 1];
while (true) {
DWORD dwBytesRead;
BOOL bRead;
bRead = InternetReadFile(
hHttpFile,
buffer,
dwFileSize + 1,
&dwBytesRead);
if (dwBytesRead == 0) break;
if (!bRead) {
printf("InternetReadFile error : <%lu>\n", GetLastError());
}
else {
buffer[dwBytesRead] = 0;
printf("Retrieved %lu data bytes: %s\n", dwBytesRead, buffer);
}
}
InternetCloseHandle(hHttpFile);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return 0;
}
@AhnMo
Copy link
Author

AhnMo commented Dec 16, 2017

Add custom header: HttpAddRequestHeaders

...
	0);

WCHAR szHeader[BUFSIZ] = { 0 };

lstrcpy(szHeader, L"User-Agent: Mozilla/5.0\r\n");
lstrcat(szHeader, L"\r\n");

HttpAddRequestHeaders(hHttpFile, szHeader, -1, HTTP_ADDREQ_FLAG_ADD);

while (!HttpSendRequest(hHttpFile, NULL, 0, 0, 0)) {
...

@AhnMo
Copy link
Author

AhnMo commented Dec 20, 2017

https://msdn.microsoft.com/ko-kr/library/windows/desktop/aa384233(v=vs.85).aspx

Wininet API make cache automatically, we must use INTERNET_FLAG_RELOAD flag to prevent fetch page from cache..

@soonercpp
Copy link

Wow! Thanks so much AhnMo for posting this complete yet simple WinINet code that works! With only a couple of slight modifications was able to get this working with my requirements. Thanks again for doing this for the community. Best wishes!

@aricsoftware
Copy link

This is nice! Is there a way to pass parameters using these same functions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment