
The first class implements an MFC CDialog derived class which
performs HTTP downloads similar to the Internet Explorer download dialog as
shown below:

To use CHttpDownloadDlg in your project simply include
HttpDownloadDlg.cpp/h from the test application in your application and #include
"HttpDownloadDlg.h" in whichever files you want to use the class in.
To bring up the dialog to download a specific file, just use some code like the
following:
CHttpDownloadDlg dlg;
dlg.m_Settings.m_sURLToDownload = _T("http://www.some-site.com/somefile.ext");
dlg.m_Settings.m_sFileToDownloadInto = _T("c:\\somefile.ext");
if (dlg.DoModal() == IDOK)
AfxMessageBox(_T("File was downloaded successfully"));
The CHttpDownloader class is used internally by CHttpDownloadDlg and allows
you to operate in a synchronous non-UI
manner as follows:
CHttpDownloader downloader;
downloader.m_Settings.m_sURLToDownload = _T("http://www.some-site.com/somefile.ext");
downloader.m_Settings.m_sFileToDownloadInto = _T("c:\\somefile.ext");
downloader.Download();
Finally if you want to use the WinHttp stack instead of Wininet, then you can
use CHttpDownloadDlg2 and CHttpDownloader2 instead of CHttpDownloadDlg and
CHttpDownloader.
The enclosed
zip file contains the CHttpDownloadDlg, CHttpDownloader, CHttpDownloadDlg2 &
CHttpDownloader2 source code and 2
simple MFC dialog based programs which exercises the classes.
Copyright
- You are allowed to include the source code in
any product (commercial, shareware, freeware or otherwise) when your product
is released in binary form.
- You are allowed to modify the source code in
any way you want except you cannot modify the copyright details at the top
of each module.
- If you want to distribute source code with
your application, then you are only allowed to distribute versions released
by the author. This is to maintain a single distribution point for the
source code.
Updates
V1.0 (14 November 1999)
V1.01 (15 November 1999)
- Updated documentation to refer to what rc resources are
needed by the class.
- Renamed all resources used from "ASYNC_DOWLOAD"
to include "HTTPDOWNLOAD".
- Now handles HTTP error 407 (Proxy Authentication) and
HTTP error 401 (Server Authentication). You are prompted with a password
dialog when either of these forms of authentication are required.
- Sample program now allows you to specify the URL to
download from aswell as the file to download into and the username and
password to connect with.
- The download file is now deleted if the download was
aborted or if some other error occurs during the download.
- Parsing of URL will now work where "http://"
is not explicitely specified.
- Improved the general look and feel of the demo app and
called it "HTML File Downloader".
V1.02 (29 November 1999)
- Fixed a number of compiler warnings and errors when
used in VC++ 6.0.
V1.03 (25 January 2000)
- Fixed a problem where server authentication was not
being detected correctly, while proxy authentication was being handled.
- Updated the way and periodicity certain UI controls are
updated during the HTTP download.
V1.04 (12 June 2000)
- Now fully supports HTTPS downloads.
V1.05 (2 July 2000)
- Now supports resuming downloads
V1.06 (28 April 2001)
- Updated Copyright Information
- You need to have the Latest Wininet header files installed to compile the code. This will not be the case with VC 5, which you will also have to install the plaform SDK also.
- Now includes support for the following fields:
m_sProxyUserName: You can now specify the Proxy user details
m_sProxyPassword: THe Proxy password to use
m_ConnectionType: You can now specify the HTTP connection type
m_bPromptFileOverwrite: Should you be prompted for file overwrites
m_bPromptForProxyDetails: Should you be prompted for Proxy authentication
m_bPromptForHTTPDetails: Should you be prompted for HTTP authentication
m_sUserAgent: The User Agent value to use in HTTP requests
- Fixed a problem in downloading files using HTTPS.
- Provided a OnSetOptions method to allow customisation through deriving your own
class from CHttpDownloadDlg.
V1.07 (17 June 2001)
- Now provides Bandwidth throtling support. Thanks to
Karim Mribti for this good addition.
- Code now uses InternetReadFile instead of
InternetReadFileEx as InternetReadFileEx is not implemented under Unicode.
V1.08 (10 October 2001)
- Fixed a problem where old style MFC exception handling
was being used instead of C++ standard.
- Code is now independent of Platform SDK meaning that it can compile on VC 5 without need for having the Platform SDK installed.
V1.09 (3 December 2001)
- Tidied up support for resumed downloads.
V1.10 (2 February 2002)
- Fixed a bug where the local downloaded file would get
appended to even when you are not resuming the download. Thanks to Davide
Zaccanti for spotting this problem.
- Updated documentation to include explicit requirements
for the class.
V1.11 (20 April 2002)
- Fixed bug in call to AfxBeginThread. Thanks to mark
anderson for spotting this problem.
- Fixed a redraw glitch where the progress control was
being updated twice instead of once each time for each percentage. Again
thanks to Mark Anderson for spotting this problem.
V1.12 (30 October 2002)
- Fixed a problem with seeking to the correct position when resuming a download. Thanks to Liping Dai for this bug report.
V1.13 (6 January 2003)
- Fixed a bug where using HTTPS on a non standard port
would cause the download to fail.Thanks to "Sundar" for spotting this problem
- Fixed a bug in the displaying of errors which use the
strings "IDS_HTTPDOWNLOAD_INVALID_SERVER_RESPONSE" and "IDS_HTTPDOWNLOAD_INVALID_SERVER_RESPONSE"
V1.14 (19 January 2003)
- Fixed a bug in calling InternetReadFile when doing
proxy authentication which could have resulted in an infinite loop occurring.
Thanks to Nathan Lewis for reporting this.
- Reworked the code which queries for the HTTP status
code to use the HTTP_QUERY_FLAG_NUMBER flag in the call to HttpQueryInfo.
Again thanks to Nathan Lewis for suggesting this.
- Reworked the code which queries for the HTTP content
length code to use the HTTP_QUERY_FLAG_NUMBER flag in the call to
HttpQueryInfo. Again thanks to Nathan Lewis for suggesting this.
V1.15 (29 June 2003)
- The progress control now uses a range of 0 - 100
percent rather than the actual file size. This prevents overflow with the
SetRange method. I could use SetRange32, but then the class would depend on
having an updated version of the common control dll installed.
- Fixed a problem in OnStatusCallBack when compiled for
Unicode.
V1.16 (5 February 2004)
- Updated the way the progress control is displayed when
a resumed download is performed. Thanks to David C. Hochrein for reporting
this addition.
V1.17 (12 February 2004)
- Class now automatically pulls in wininet.lib support
via a #pragma. This means that client apps no longer need to update the linker
settings.
V1.18 (11 November 2004)
- Fixed an issue in the code where the request is only reissued when authentication errors occur and the code needs to prompt the user. Thanks to Thomas Kiesswetter for reporting this problem and provided the fix.
- Addition of a CHTTPDOWNLOADDLG_EXT_CLASS preprocessor macro which allows the class to be easily added to an extension dll
- Fixed a bug when errors are displayed when QueryStatusCode fails.
V1.19 (5 March 2005)
- Tidied up the way the file to be downloaded is closed and deleted if the download fails. Thanks to Guillermo for reporting this bug.
- Fixed a bug in the main logic of the class which caused the http or proxy credentials dialog to be continually displayed even when you hit cancel. Thanks to Guillermo for reporting this bug and providing the fix.
- Optimized the code which calls GetLastError.
- Display of error messages now uses FormatMessage to display a more friendlier value.
V1.20 (4 April 2005)
- Fix for bug when providing all information in the dialog for both proxy and user validation and one uses the option to connect directly via the proxy and no prompting is checked then when the code 407 is received the code resends with proxy info and gets a 401 code. This time it reports the error as the bool flag for bRetrying is true. Thanks to Stephen Miller for providing the fix for this problem.
V1.21 (8 May 2006)
- Updated copyright details.
- Fixed an ASSERT in CHttpDownloadDlg::OnThreadFinished when you cancel a
download prematurely.
- Updated the documentation to use the same style as the web site.
- Did a spell check of the documentation.
- Addition of a m_bNoURLRedirect variable so that the class can
optionally ignore 302 redirects.
- The flags passed to HttpOpenRequest can now be customized via a new
method GetOpenRequestFlags
- Optimized creation of member variables in CHttpDownloadDlg constructor.
- Updated various captions and version infos in the sample app
- Fixed a number of compiler warnings when the code is compiled using
/Wp64. Please note that to implement these fixes, the code will now require
the Platform SDK to be installed if you compile the code on VC 6.
V1.22 (2 July 2006)
- Updated the code to clean compile on VC 2005
- Code now uses new C++ style casts rather than old style C casts where
necessary.
- The class now requires the Platform SDK if compiled using VC 6.
V1.23 (15 July 2006)
- Fixed some issues in OnStatusCallback when the code is compiled for
Unicode. Thanks to Itamar Syn-Hershko for reporting this issue.
- WM_HTTPDOWNLOAD_THREAD_FINISHED now uses WM_USER instead of WM_APP to
define its value
- Major refactoring of the code into 3 classes namely
CHttpDownloadSettings, CHttpDownloader and the existing CHttpDownloadDlg
class which has been significantly reworked to fit in with this new layout.
The new layout means that you can use the new "CHttpDownloader" class in a
synchronous manner without any UI at all. For example to download a file
without any UI you would just use something like:
CHttpDownloader downloader;
downloader.m_Settings.m_sURLToDownload = _T("http://www.some-site.com/somefile.ext");
downloader.m_Settings.m_sFileToDownloadInto = _T("c:\\somefile.ext");
downloader.Download();
When the Download() function returns the file will have been downloaded
successfully or will have failed to download. Various synchronous virtual
functions can be overridden to handle the various events as they occur
during the download. The CHttpDownloadDlg class
now derives from both CDialog and CHttpDownloader, and handles these virtual events
to provide the existing functionality which it had. You should review how
CHttpDownloadDlg handles these virtual functions if you want to create your
own customized implementation which derives from CHttpDownloader.
- Fixed a spelling mistake in the IDS_HTTPDOWNLOAD_RETREIVEING_FILE string
- The buffer size used in calls to InternetReadFile can now be changed via
CHttpDownloadSettings::m_dwReadBufferSize
- Thread protected changes to the Abort flag
V1.24 (16 July 2006)
- Split off CHttpDownloader class into its own module of
HttpDownloader.cpp/h.
- Implemented a WinHttp version of CHttpDownloader called
CHttpDownloader2.
- Implemented a WinHttp version of CHttpDownloadDlg called
CHttpDownloadDlg2.
- Now includes a similar sample app in the download to demonstrate the
WinHttp functionality.
- Now includes support for customizing the HTTP_REFERRER header
- Now includes support for customizing the HTTP verb used e.g. "GET" etc
- Now includes support for specifying the HTTP version to use e.g.
"HTTP/1.0". "HTTP/1.1" etc
- Now includes support for customizing the HTTP_ACCEPT header
- Added support for client customization of any other HTTP headers which
are now transmitted
- Brought the CHttpDownloadSettings member variables back into the
CHttpDownloader class. This means that the HttpDownloadSettings module is
now defunct.
- In line with the new CHttpDownloader2 class, the creation of the various
handles in now broken down into 3 virtual functions of CreateSession,
CreateConnection and CreateRequest.
- Now supports both HTTP and Proxy preauthentication.
- Reviewed all TRACE statements for correctness
- internet handles are now freed up before CHttpDownloader::Download
returns
- Moved the m_lAbort flag from the CHttpDownloadDlg up into
CHttpDownloader. This allows client code extra flexibility in how users can
cancel a download. Thanks to Itamar Syn-Hershko for reporting this issue.
V1.25 (25 August 2006)
- Downloading to file is now optional for both CHttpDownloader and
CHttpDownloader2, a new virtual function OnDownloadData is now called each
time a chunk of data has been retrieved. The default implementation writes
the data into the file "m_sFileToDownloadInto". A derived class is free to
override this function and implement their own specific functionality.
Thanks to Itamar Syn-Hershko for suggesting this update.
- Optimized the way aborting a download occurs by freeing up the handles
in the SetAbort call. This should hopefully fix times where aborting a
download appears to respond slowly.
- Class now supports sending client certificates via user selection and
programmatically. Thanks to Johan Sörensen for this very nice addition.
V1.26 (17 September 2006)
- Fixed an issue where the OnStatusCallback method was not being called.
This has been addressed by renaming the function to
CHttpDownloadDlg2::OnWinHttpStatusCallBack. This is necessary due to the
refactoring of the base CHttpDownloader2 class. Thanks to Johan Sörensen for
reporting this bug
- Fixed an issue where the OnStatusCallback method was not being called.
This has been addressed by renaming the function to
CHttpDownloadDlg::OnWininetStatusCallBack. This is necessary due to the
refactoring of the base CHttpDownloader class. Thanks to Johan Sörensen for
reporting this bug
V1.27 (10 June 2007)
- Updated comments in documentation about usage of the Platform SDK.
- Updated copyright details.
- Updated CHttpDownloadDlg::OnWininetStatusCallBack in line with
CFTPTransferer class.
- CHttpDownloader now supports resumed downloads for files > 4GB in size.
- CHttpDownloader2 now supports resumed downloads for files > 4GB in size.
- Updated sample app to compile cleanly on VC 2005
V1.28 (30 December 2007)
- Updated sample apps to clean compile on VC 2008
- Remove VC 6 style appwizard comments from the code.
- CHttpDownloader::GetErrorMessage now uses the
FORMAT_MESSAGE_IGNORE_INSERTS flag. For more information please see Raymond
Chen's blog at
http://blogs.msdn.com/oldnewthing/archive/2007/11/28/6564257.aspx.
Thanks to Alexey Kuznetsov for reporting this issue.
- CHttpDownloader::GetErrorMessage now uses Checked::tcsncpy_s if compiled
using VC 2005 or later.
- CHttpDownloader2::GetErrorMessage now uses the
FORMAT_MESSAGE_IGNORE_INSERTS flag. For more information please see Raymond
Chen's blog at
http://blogs.msdn.com/oldnewthing/archive/2007/11/28/6564257.aspx.
Thanks to Alexey Kuznetsov for reporting this issue.
- CHttpDownloader2::GetErrorMessage now uses Checked::tcsncpy_s if
compiled using VC 2005 or later.
V1.29 (11 May 2008)
- Updated the logic in CHttpDownloadDlg::OnWininetStatusCallBack to
correctly handle ASCII or Unicode strings. Thanks to Hans Dietrich for
prompting this update.
- Updated copyright details
- Addition of AttachSession and DetachSession methods which allow the
lifetime of the session to be controlled independently of the lifetime of
the CHttpDownloader(2) instance. Thanks to Hans Dietrich for prompting this
update
- Fixed a spelling mistake in the IDS_HTTPDOWNLOAD_RETRIEVING_FILE string
resource
- OnProgress virtual methods now correctly handles reporting results for
resumed downloads
V1.30 (08 June 2008)
- Updated code to compile correctly using _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
define
- Code now compiles cleanly using Code Analysis (/analyze)
- The code has now been updated to support VC 2005 or later only.