HTTP Requests using WinINet
This section of the WinINet API reference documentation details the functions and concepts related to making HTTP requests from Windows applications.
Introduction to HTTP with WinINet
WinINet provides a high-level API for developing client applications that communicate with Internet servers. It simplifies the process of handling protocols like HTTP, FTP, and Gopher.
Core Functions for HTTP Requests
The following functions are fundamental to performing HTTP requests:
InternetOpen: Initializes an application's use of the WinINet functions.InternetConnect: Establishes a connection to a specified Internet server.HttpOpenRequest: Opens an HTTP request handle to the specified HTTP server.HttpSessionSendRequest: Sends the request to the server.InternetReadFile: Reads data from the specified Internet handle.InternetCloseHandle: Closes an open Internet handle.
Key Concepts
- Internet Handles: WinINet uses handles to represent various Internet resources and sessions.
- Request Headers: You can add custom headers to your HTTP requests using functions like
HttpAddRequestHeaders. - Response Status: After sending a request, you can retrieve the HTTP status code to determine the success or failure of the operation.
- Redirection: WinINet handles HTTP redirections (e.g., 301, 302 status codes) automatically by default.
Example Workflow
- Call
InternetOpento get an HINTERNET session handle. - Call
InternetConnectto establish a connection to the target server. - Call
HttpOpenRequestto create an HINTERNET request handle. - Optionally, add request headers using
HttpAddRequestHeaders. - Call
HttpSendRequestto send the request to the server. - Check the HTTP status code using
HttpQueryInformationHttpServer. - Use
InternetReadFileto retrieve the response body. - Close all opened handles using
InternetCloseHandle.
InternetOpen
Initializes an application's use of the WinINet functions. This function also establishes a connection to a specified FTP or HTTP server. Typically, an application calls InternetOpen once.
BOOL InternetOpen(
[in] LPCTSTR lpszAgent,
[in] DWORD dwAccessType,
[in] LPCTSTR lpszProxyName,
[in] LPCTSTR lpszProxyBypass,
[in] DWORD dwFlags
);
InternetConnect
Establishes a connection to a specified Internet server. This function can establish a connection for FTP, HTTP, or a custom protocol.
HINTERNET InternetConnect(
[in] HINTERNET hInternet,
[in] LPCTSTR lpszServerName,
[in] INTERNET_PORT InternetInPort,
[in] LPCTSTR lpszUsername,
[in] LPCTSTR lpszPassword,
[in] DWORD dwService,
[in] DWORD dwFlags,
[in] DWORD_PTR dwContext
);
HttpOpenRequest
Opens an HTTP request handle to the specified HTTP server. This function can be used to create any type of HTTP request, including GET, POST, PUT, DELETE, etc.
HINTERNET HttpOpenRequest(
[in] HINTERNET hConnect,
[in] LPCTSTR lpszVerb,
[in] LPCTSTR lpszObjectName,
[in] LPCTSTR lpszVersion,
[in] LPCTSTR lpszReferer,
[in] LPCTSTR *lplpszAcceptTypes,
[in] DWORD dwFlags,
[in] DWORD_PTR dwContext
);
HttpSessionSendRequest
Sends the request to the server specified by the hRequest handle. This function can also send additional headers and data to the server.
BOOL HttpSessionSendRequest(
[in] HINTERNET hRequest,
[in] LPCTSTR lpszHeaders,
[in] DWORD dwHeadersLength,
[in] LPVOID lpOptional,
[in] DWORD dwOptionalLength,
[in] DWORD dwTotalLength,
[in] DWORD_PTR dwContext
);
InternetReadFile
Reads data from the specified Internet handle. This function is used to retrieve the response data from the server after sending an HTTP request.
BOOL InternetReadFile(
[in] HINTERNET hFile,
[out] LPVOID lpBuffer,
[in] DWORD dwNumberOfBytesToRead,
[out] LPDWORD lpdwNumberOfBytesRead
);
InternetCloseHandle
Closes an open Internet handle. It is important to close all handles when they are no longer needed to free up system resources.
BOOL InternetCloseHandle(
[in] HINTERNET hInternet
);