The WinHttpConnect function specifies the initial target server of an HTTP request and returns an HINTERNET connection handle to an HTTP session for that initial target.
WINHTTPAPI HINTERNET WinHttpConnect( IN HINTERNET hSession, IN LPCWSTR pswzServerName, IN INTERNET_PORT nServerPort, IN DWORD dwReserved );
Pointer to a null-terminated string that contains the host name of an HTTP server. Alternately, the string can contain the IP address of the site in ASCII, for example, 10.0.1.45. Note that WinHttp does not accept international host names without converting them first to Punycode. For more information, see Handling Internationalized Domain Names (IDNs).
Unsigned integer that specifies the TCP/IP port on the server to which a connection is made. This parameter can be any valid TCP/IP port number, or one of the following values.
||Uses the default port for HTTP servers (port 80).|
||Uses the default port for HTTPS servers (port 443). Selecting this port does not automatically establish a secure connection. You must still specify the use of secure transaction semantics by using the WINHTTP_FLAG_SECURE flag with WinHttpOpenRequest.|
||Uses port 80 for HTTP and port 443 for Secure Hypertext Transfer Protocol (HTTPS).|
This parameter is reserved and must be 0.
Returns a valid connection handle to the HTTP session if the connection is successful, or NULL otherwise. To retrieve extended error information, call GetLastError. Among the error codes returned are the following.
||The type of handle supplied is incorrect for this operation.|
||An internal error has occurred.|
||The URL is invalid.|
||The operation was canceled, usually because the handle on which the request was operating was closed before the operation completed.|
||The URL scheme could not be recognized, or is not supported.|
||The WinHTTP function support is being shut down or unloaded.|
||Not enough memory was available to complete the requested operation. (Windows error code)|
Even when WinHTTP is used in asynchronous mode (that is, when WINHTTP_FLAG_ASYNC has been set in WinHttpOpen), this function operates synchronously. The return value indicates success or failure. To get extended error information, call GetLastError.
WinHttpConnect specifies the target HTTP server, however a response can come from another server if the request was redirected. You can determine the URL of the server sending the response by calling WinHttpQueryOption with the WINHTTP_OPTION_URL flag.
The following example shows how to use secure transaction semantics to download a resource from an HTTPS server. The sample code initializes the Microsoft Windows HTTP Services (WinHTTP) application programming interface (API), selects a target HTTPS server, then opens and sends a request for this secure resource.
WinHttpQueryDataAvailable is used with the request handle to determine how much data is available for download, then WinHttpReadData is used to read that data. This process repeats until the entire document has been retrieved and displayed.
DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; BOOL bResults = FALSE; HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
|Windows version||Windows XP, Windows 2000 Professional with SP3 [desktop apps only] Windows Server 2003, Windows 2000 Server with SP3 [desktop apps only]|