WinINet API Reference

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:

Key Concepts

Example Workflow

  1. Call InternetOpen to get an HINTERNET session handle.
  2. Call InternetConnect to establish a connection to the target server.
  3. Call HttpOpenRequest to create an HINTERNET request handle.
  4. Optionally, add request headers using HttpAddRequestHeaders.
  5. Call HttpSendRequest to send the request to the server.
  6. Check the HTTP status code using HttpQueryInformationHttpServer.
  7. Use InternetReadFile to retrieve the response body.
  8. 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
);
            

Further Reading