HttpConnectUrl Function  
 
HCLIENT WINAPI HttpConnectUrl(
  LPCTSTR lpszUrl,  
  UINT nTimeout,  
  DWORD dwOptions  
);

The HttpConnectUrl function establishes a connection with the specified server using a URL.

Parameters

lpszUrl
A null-terminated string which specifies the URL for the server. The URL must follow the conventions for the Hypertext Transfer Protocol and may specify either a standard or secure connection, alternate port number, username, password and optional working directory.
nTimeout
The number of seconds that the client will wait for a response from the server before failing the current operation.
dwOptions
An unsigned integer that specifies one or more options. This parameter is constructed by using a bitwise operator with any of the following values:
Value Description
HTTP_OPTION_NOCACHE This instructs the server to not return a cached copy of the resource. When connected to an HTTP 1.0 or earlier server, this directive may be ignored.
HTTP_OPTION_KEEPALIVE This instructs the server to maintain a persistent connection between requests. This can improve performance because it eliminates the need to establish a separate connection for each resource that is requested.
HTTP_OPTION_REDIRECT This option specifies the client should automatically handle resource redirection. If the server indicates that the requested resource has moved to a new location, the client will close the current connection and request the resource from the new location. Note that it is possible that the redirected resource will be located on a different server.
HTTP_OPTION_PROXY This option specifies the client should use the default proxy configuration for the local system. If the system is configured to use a proxy server, then the connection will be automatically established through that proxy; otherwise, a direct connection to the server is established. The local proxy configuration can be changed in the system settings or control panel.
HTTP_OPTION_ERRORDATA This option specifies the client should return the content of an error response from the server, rather than returning an error code. Note that this option will disable automatic resource redirection, and should not be used with HTTP_OPTION_REDIRECT.
HTTP_OPTION_NOUSERAGENT This option specifies that the client should not include a User-Agent header with requests made during the session. The User-Agent header is typically used by servers to identify the client application, operating system and other platform information. This option may be used when the server does not require a User-Agent header.
HTTP_OPTION_HTTP2 This option specifies the client should attempt a HTTP/2 connection with the server. If a connection cannot be established using HTTP/2 the client will attempt to connect using an earlier version of the protocol. This option will always establish a secure connection with the server. If this option is not specified, the connection will use HTTP/1.1 as the default protocol version.
HTTP_OPTION_HTTP3 This option specifies the client should attempt a HTTP/3 (QUIC) connection with the server. If a connection cannot be established using HTTP/3 the client will attempt a connection using an earlier version of the protocol. You should only use this option if you know the server supports the QUIC protocol, otherwise requests will be delayed as a result of the fallback to attempting a HTTP/2 connection.
HTTP_OPTION_TUNNEL This option specifies that a tunneled TCP connection and/or port-forwarding is being used to establish the connection to the server. This changes the behavior of the client with regards to internal checks of the destination IP address and remote port number, default capability selection and how the connection is established.
HTTP_OPTION_TRUSTEDSITE This option specifies the server is trusted. The server certificate will not be validated and the connection will always be permitted. This option only affects connections using the TLS protocol.
HTTP_OPTION_SECURE This option specifies the client should attempt to establish a secure connection with the server. The server must support secure connections using the TLS protocol and the client will default to using TLS 1.2 or later. If HTTP_PORT_DEFAULT is specified as the remote port, this option will automatically change the port number to use HTTP_PORT_SECURE instead.
HTTP_OPTION_SECURE_FALLBACK This option specifies the client should permit the use of less secure cipher suites for compatibility with legacy servers. If this option is specified, the client will allow connections using TLS 1.0 and cipher suites that use RC4, MD5 and SHA1. Although this option may permit connections with servers which do not support current standards, avoid using it unless absolutely necessary. Some servers may reject the connection if the client offers to use TLS 1.0, even when TLS 1.2 or later is also supported.
HTTP_OPTION_PREFER_IPV6 This option specifies the client should prefer the use of IPv6 if the server hostname can be resolved to both an IPv6 and IPv4 address. This option is ignored if the local system does not have IPv6 enabled, or when the hostname can only be resolved to an IPv4 address. If the server hostname can only be resolved to an IPv6 address, the client will attempt to establish a connection using IPv6 regardless if this option has been specified.
HTTP_OPTION_FREETHREAD This option specifies the handle returned by this function may be used by any thread, and is not limited to the thread which created it. The application is responsible for ensuring that access to the handle is synchronized across multiple threads.
HTTP_OPTION_HIRES_TIMER This option specifies the elapsed time for data transfers should be returned in milliseconds instead of seconds. This will return more accurate transfer times for smaller amounts of data over fast network connections.

Return Value

If the function succeeds, the return value is a handle to a client session. If the function fails, the return value is INVALID_CLIENT. To get extended error information, call HttpGetLastError.

Remarks

The HttpConnectUrl function uses an HTTP URL to establish a connection with a server. The URL must be in the following format:

[http|https]://[username : password] @] hostname [:port] / [path / ...] [filename]

If no user name and password is provided, then the client session will be authenticated as an anonymous user. The URL scheme will always determine if the connection is secure, not the option. In other words, if the "http" scheme is used and the HTTP_OPTION_SECURE option is specified, that option will be ignored. To establish a secure connection, the "https" scheme must be specified. The HttpValidateUrl function can be used to verify that a URL is valid prior to calling this function.

The HttpConnectUrl function is designed to provide a simpler, more convenient interface to establishing a connection with a server. However, complex connections such as those using a proxy server or a secure connection which uses a client certificate will require the program to use the lower-level connection functions. If you only need to upload or download a file using a URL, then refer to the HttpUploadFile and HttpDownloadFile functions.

This function will block the current thread until a connection has been established or the timeout period has elapsed. To prevent it from blocking the main user interface thread, the application should create a background worker thread and establish a connection by calling HttpConnectUrl in that thread. If the application requires multiple simultaneous connections, it is recommended you create a worker thread for each connection.

Most applications should use HTTP_VERSION_DEFAULT as the value for the dwVersion parameter. This will default to an appropriate version for the Windows platform and ensures the broadest compatibility with most servers. If your application specifies HTTP_VERSION_20, a secure connection using TLS 1.2 or later will always be used. The minimum required platform for HTTP/2 support is Windows 10 (Build 1903) or Windows Server 2019. Earlier versions of the Schannel SSP do not support the features required for a secure HTTP/2 connection.

If the HTTP_OPTION_KEEPALIVE option is specified and the server does not support persistent connections, the client will automatically reconnect when each resource is requested. Although it will not provide any performance benefits, this allows the option to be used with all servers. This option is automatically enabled when using HTTP/2.

Using the HTTP_OPTION_NOUSERAGENT option to prevent sending a User-Agent header does not make requests anonymous. Servers may still identify clients based on IP address information, authentication credentials, request patterns or other HTTP headers included with the request.

If your application specifies the HTTP_OPTION_HTTP2 option, a secure connection using TLS 1.2 or later will always be used. The minimum required platform for HTTP/2 support is Windows 10 (Build 1903) or Windows Server 2019. Earlier versions of Windows do not support the features required for a secure HTTP/2 connection. If the server only accepts earlier versions of the protocol, the client will attempt to automatically downgrade the request to HTTP/1.1. If a connection using an earlier version of the protocol cannot be established, the function will fail and return INVALID_CLIENT.

You should only attempt to use HTTP/3 if you know the server supports that version of the protocol. If the server does not support HTTP/3, the client will wait for the timeout period to elapse to determine the server is not responding to the HTTP/3 request and will then attempt to use HTTP/2. If you specify HTTP/3 as the protocol version and you are experiencing long delays for requests the complete, the most likely reason is the server does not support the QUIC protocol and every request is falling back to use HTTP/2 instead. The minimum required platform for HTTP/3 support is Windows 11 21H2 and later builds or Windows Server 2022 or later.

The handle returned by this function is initially attached to the thread that created it. From that point on, until the handle is released, only the owner may call functions using that handle. The ownership of the handle may be transferred from one thread to another using the HttpAttachThread function.

Specifying the HTTP_OPTION_FREETHREAD option enables any thread to call any function using the handle, regardless of which thread created it. It is important to note that this option disables certain internal safety checks which are performed by the library and may result in unexpected behavior unless access to the handle is synchronized. If one thread calls a function in the library, it must ensure that no other thread will call another function at the same time using the same handle.

Example

HCLIENT hClient;
LPCTSTR lpszUrl = _T("https://www.example.com/");

// Connect to the site specified by the URL
hClient = HttpConnectUrl(lpszUrl, HTTP_TIMEOUT, HTTP_OPTION_DEFAULT);

if (hClient == INVALID_CLIENT)
{
    TCHAR szError[128];

    // Display a message box that describes the error
    HttpGetErrorString(HttpGetLastError(), szError, 128);
    MessageBox(NULL, szError, NULL, MB_ICONEXCLAMATION | MB_TASKMODAL);
    return;
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtpv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also