This article describes the implementation of C + + download method, share for everyone to reference.
The specific implementation code is as follows:
Copy Code code as follows:
#include <UrlMon.h>
#include <WinInet.h>
#pragma comment (lib, "WinInet")
void Cfiledownloaddlg::onbnclickedbtndownload ()
{
Using the Urldownloadtofile function
HRESULT Hret = Urldownloadtofile (NULL, "Http://www.baidu.com/img/baidu_sylogo1.gif", "C:\\temp\\1.gif", 0,null);
if (S_OK!= Hret)
{
MessageBox ("Download Failed");
Return
}
Using the Windows Internet Library
Hinternet hsession = InternetOpen ("", internet_open_type_preconfig, NULL, NULL, 0);
if (hsession!= NULL)
{
Hinternet hLink2 = InternetOpenUrl (hsession, "Http://www.baidu.com/img/baidu_sylogo1.gif", NULL, 0, Internet_flag_ Dont_cache, 0);
if (hLink2!= NULL)
{
BYTE temp[1024];
DWORD dwnum = 1;
FILE *hfile;
if ((hfile = fopen ("C:\\temp\\2.gif", "WB")!= NULL)
{
while (dwnum>0)
{
InternetReadFile (HLINK2, temp, 1024, &dwnum);
Fwrite (temp, sizeof (char), dwnum, hfile);
}
Fclose (hfile);
MessageBox ("Download finished ...");
}
InternetCloseHandle (HLINK2);
HLINK2 = NULL;
}
InternetCloseHandle (hsession);
Hsession = NULL;
}
}
I hope this article will help you with the C + + program design.