The HttpDownloadFileEx function provides a convenient way
for an application to download a file in a single function call.
Based on the connection information specified in the URL, it will
connect to the server, authenticate the session and then download the
file to the local system. The URL must be complete, and specify
either a standard or secure HTTP scheme:
[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.
If your application requests a HTTP/2 or HTTP/3 connection and 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 a value of zero.
The lpStatus parameter can be used by the application to
determine the final status of the transfer, including the total
number of bytes copied, the amount of time elapsed and other
information related to the transfer process. If this information
isn't needed, then this parameter may be specified as NULL.
The lpEventProc parameter specifies a pointer to a
function which will be periodically called during the file transfer
process. This can be used to check the status of the transfer by
calling HttpGetTransferStatus and then update the program's
user interface. For example, the callback function could calculate
the percentage for how much of the file has been transferred and then
update a progress bar control. The dwParam parameter is used
in conjunction with the event handler and specifies a user-defined
value that is passed to the callback function. One common use in a
C++ program is to pass the this pointer as the value, and then
cast it back to an object pointer inside the callback function. If no
event handler is required, then a NULL pointer can be specified as
the value for lpEventProc and the dwParam parameter
will be ignored.
The HttpDownloadFileEx function is designed to provide a
simpler interface for downloading a file. However, complex
connections such as those using a specific proxy server or a secure
connection which uses a client certificate will require the program
to establish the connection using HttpConnect or
HttpProxyConnect and then use HttpGetFile to download
the file.
The HTTPTRANSFERSTATUSEX structure supports files larger than 4 GiB
(4,294,967,296 bytes) by using ULARGE_INTEGER members to represent
the total file size and the number of bytes transferred. These members
provide 64-bit unsigned integer values that can be accessed using
the QuadPart member, or as two 32-bit values using the LowPart
and HighPart members. For languages that support a native unsigned
64-bit integer type, that type may be used in place of ULARGE_INTEGER when
declaring the structure, provided it has the same size, alignment,
and memory representation. The remaining members of the structure
retain their original data types.
HTTPTRANSFERSTATUSEX httpStatus;
LPCTSTR lpszLocalFile = _T("c:\\temp\\database.sql");
LPCTSTR lpszRemoteFile = _T("https://www.example.com/updates/database.sql");
BOOL bResult;
// Download the file using the specified URL
bResult = HttpDownloadFileEx(lpszLocalFile,
lpszRemoteFile,
HTTP_OPTION_DEFAULT,
&httpStatus,
NULL, 0);
if (!bResult)
{
TCHAR szError[128];
// Display a message box that describes the error
HttpGetErrorString(HttpGetLastError(), szError, 128);
MessageBox(NULL, szError, NULL, MB_ICONEXCLAMATION|MB_TASKMODAL);
return;
}