Last active
September 17, 2024 17:14
-
-
Save AhnMo/5cf37bbd9a6fa2567f99ac0528eaa185 to your computer and use it in GitHub Desktop.
Wininet HTTP Client Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is nice! Is there a way to pass parameters using these same functions?