WinINet API
The WinINet API provides high-level access to the HTTP and FTP protocols. It is designed for applications that need to retrieve documents from the Internet, such as web browsers.
Overview
WinINet simplifies the process of making requests to Internet servers. It handles many of the complexities associated with network communication, including:
- Establishing connections to Internet servers.
- Sending and receiving data.
- Managing HTTP headers and status codes.
- Handling authentication.
- Supporting proxy servers.
Key Concepts
Internet Sessions
An Internet session is established using the InternetOpen function. This function initializes the WinINet library and provides a handle to the session.
HINTERNET InternetOpen(
LPCWSTR lpszAgent,
DWORD dwAccessType,
LPCWSTR lpszProxyName,
LPCWSTR lpszProxyBypass,
DWORD dwFlags
);
Connections
After establishing an Internet session, you can open a connection to a specific Internet server using the InternetConnect function.
HINTERNET InternetConnect(
HINTERNET hInternet,
LPCWSTR lpszServerName,
INTERNET_PORT nPort,
LPCWSTR lpszUsername,
LPCWSTR lpszPassword,
DWORD dwService,
DWORD dwFlags,
DWORD_PTR dwContext
);
Requests
To retrieve data from a server, you can open a request using the HttpOpenRequest function and then send it using HttpSendRequest.
HINTERNET HttpOpenRequest(
HINTERNET hConnect,
LPCWSTR lpszVerb,
LPCWSTR lpszObjectName,
LPCWSTR lpszVersion,
LPCWSTR lpszReferer,
LPCWSTR* lplpszAcceptTypes,
DWORD dwFlags,
DWORD_PTR dwContext
);
BOOL HttpSendRequest(
HINTERNET hRequest,
LPCWSTR lpszHeaders,
DWORD dwHeadersLength,
LPVOID lpOptional,
DWORD dwOptionalLength,
DWORD dwFlags,
DWORD_PTR dwContext
);
Reading Data
Once a request has been sent and the server has responded, you can read the response data using the InternetReadFile function.
BOOL InternetReadFile(
HINTERNET hFile,
LPVOID lpBuffer,
DWORD dwNumberOfBytesToRead,
LPDWORD lpdwNumberOfBytesRead
);
Error Handling
WinINet functions return specific error codes that can be retrieved using GetLastError. The InternetGetLastResponseInfo function can provide more detailed information about the last HTTP error.
Functions
| Function | Description |
|---|---|
InternetOpen |
Initializes an application's use of the WinINet API. |
InternetConnect |
Establishes a connection to the specified Internet server. |
HttpOpenRequest |
Opens an HTTP request handle. |
HttpSendRequest |
Sends the request to the HTTP server. |
InternetReadFile |
Reads data from the specified Internet handle. |
InternetCloseHandle |
Closes the specified Internet handle. |