WinINet API Reference
The WinINet (Windows Internet) API provides high-level functions for interacting with HTTP and FTP protocols. It is primarily intended for client applications requiring Internet functionality.
Contents
Functions
InternetOpenInitializes an application's use of WinINet functions.
InternetConnectCreates a connection handle for an HTTP server.
HttpOpenRequestCreates an HTTP request handle.
HttpSendRequestSends the specified request to the HTTP server.
InternetReadFileReads data from a handle opened by WinINet.
InternetCloseHandleCloses an Internet handle.
InternetSetOptionSets an Internet option on a handle.
InternetGetLastResponseInfoRetrieves the last response code from an HTTP request.
Structures
typedef struct {
DWORD dwFlags;
DWORD dwTimeout;
DWORD dwRetryInterval;
DWORD dwCacheTimeout;
} INTERNET_BUFFERS;
Used to specify buffer information for reading and writing data.
Constants
INTERNET_OPEN_TYPE_PRECONFIG– Use system default proxy settings.INTERNET_FLAG_RELOAD– Force a fresh request, ignoring the cache.INTERNET_OPTION_CONNECT_TIMEOUT– Sets the timeout for establishing a connection.
Code Sample – Simple HTTP GET
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
int main() {
HINTERNET hSession = InternetOpen(L"MyApp",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if (!hSession) return 1;
HINTERNET hConnect = InternetConnect(hSession,
L"example.com", INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnect) { InternetCloseHandle(hSession); return 1; }
HINTERNET hRequest = HttpOpenRequest(hConnect,
L"GET", L"/", NULL, NULL, NULL,
INTERNET_FLAG_RELOAD, 0);
if (!hRequest) { InternetCloseHandle(hConnect); InternetCloseHandle(hSession); return 1; }
if (!HttpSendRequest(hRequest, NULL, 0, NULL, 0)) {
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return 1;
}
char buffer[4096];
DWORD bytesRead;
while (InternetReadFile(hRequest, buffer, sizeof(buffer) - 1, &bytesRead) && bytesRead) {
buffer[bytesRead] = 0;
printf("%s", buffer);
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return 0;
}
This example demonstrates opening a session, connecting to example.com, sending a GET request, and printing the response.