HTTP API Reference
This section provides detailed reference information for the Windows HTTP APIs, enabling developers to build powerful and efficient network-enabled applications.
Core HTTP Functions
These functions form the foundation of HTTP communication on Windows. They allow for the creation of HTTP sessions, requests, and responses.
HttpOpenRequest
Opens an HTTP request handle. This handle is used to specify the details of an HTTP request before sending it to the server.
Parameters
IN HTTPAPI_VERSION Version
: The HTTP version to use.IN PCWSTR pwszMethod
: The HTTP method (e.g., "GET", "POST").IN PCWSTR pwszObjectPath
: The path to the requested object on the server.IN PCWSTR pwszVersion
: The HTTP protocol version string (e.g., "HTTP/1.1").IN PCWSTR pwszReferer
: Optional referer header.IN PCWSTR* ppwszAcceptTypes
: Optional array of accept types.IN DWORD dwFlags
: Flags that control the behavior of the request.IN HINTERNET hConnect
: Handle to an open HTTP connection.
Return Value
Returns a handle to the HTTP request if successful, otherwise NULL
.
HttpSendRequest
Sends the specified HTTP request to the server specified by the hConnect
parameter.
Parameters
IN HINTERNET hRequest
: Handle to the HTTP request.IN DWORD dwHeadersLength
: Length of the headers to send.IN LPVOID lpHeaders
: Pointer to headers.IN DWORD dwOptionalLength
: Length of optional data.IN LPVOID lpOptional
: Pointer to optional data.
Return Value
Returns TRUE
if the request is sent successfully, FALSE
otherwise.
HttpQueryResponseHeaders
Retrieves the headers from an HTTP server response.
Parameters
IN HINTERNET hRequest
: Handle to the HTTP request.IN DWORD dwInfoLevel
: Specifies the type of header information to retrieve.OUT LPVOID lpBuffer
: Buffer to receive the headers.IN OUT LPDWORD lpdwBufferLength
: Size of the buffer.
Return Value
Returns TRUE
if headers are retrieved successfully, FALSE
otherwise.
HTTP Session Management
Manage the lifecycle of HTTP sessions and connections.
HttpConnect
Establishes a connection to an HTTP server.
Parameters
IN HINTERNET hInternet
: Handle to the Internet session.IN PCWSTR pszServerName
: The name of the server.IN INTERNET_PORTnPort
: The port number.IN PCWSTR pszVersion
: HTTP protocol version.IN DWORD dwFlags
: Connection flags.IN OUT DWORD_PTR* lpdwContext
: Application-defined context value.
Return Value
Returns a handle to the HTTP connection if successful, otherwise NULL
.
Advanced Features
Explore more advanced functionalities like handling authentication and managing cookies.
HttpSetOption
Sets HTTP-specific options for an Internet handle.
Parameters
IN HINTERNET hInternet
: Handle to the Internet object.IN DWORD dwOption
: The HTTP option to set.IN LPVOID lpBuffer
: Pointer to a buffer containing the option value.IN OUT DWORD dwBufferLength
: Size of the buffer.
Return Value
Returns TRUE
on success, FALSE
on failure.
// Example Snippet: Sending a simple GET request
HINTERNET hSession = InternetOpen(L"MyApp", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hSession) { /* Handle error */ }
HINTERNET hConnect = HttpConnect(hSession, L"www.example.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, 0, 0, NULL);
if (!hConnect) { /* Handle error */ }
HINTERNET hRequest = HttpOpenRequest(hConnect, L"GET", L"/index.html", NULL, NULL, NULL, 0, 0);
if (!hRequest) { /* Handle error */ }
if (!HttpSendRequest(hRequest, NULL, 0, NULL, 0)) { /* Handle error */ }
// ... process response ...
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);