Click here to Skip to main content
Click here to Skip to main content
Go to top

Alternative to URLDownloadToFile function

, 13 Jan 2012
Rate this:
Please Sign up or sign in to vote.
How to download files from an HTTP server.
In response to a StackOverflow question[^], I developed an alternative for URLDownloadToFile function; it is based on a generic HTTP client[^] written in C++.
CString FormatErrorMessage(DWORD dwLastError)
{
     const int nLength = 0x1000;
     TCHAR lpszBuffer[nLength] = { 0 };
 
     if (::FormatMessage(
         // FORMAT_MESSAGE_ALLOCATE_BUFFER |
         FORMAT_MESSAGE_FROM_SYSTEM,
         NULL,
         dwLastError,
         0,
         (LPTSTR) &lpszBuffer,
         nLength, NULL) == 0)
     {
         _stprintf_s(lpszBuffer, nLength, _T("Unknown INET error 0x%.4X\n"), dwLastError);
     }
     return lpszBuffer;
}
 
HRESULT DownloadURLToFile(CString strURLDownload, CString strFilePath)
{
     CHTTPClient httpClient;
     httpClient.EnableStatusCallback(TRUE);
     httpClient.InitilizePostArguments();
     httpClient.SetDownloadFile(strFilePath);
 
     BOOL bResult = httpClient.Request(
         strURLDownload,
         CHTTPClient::RequestGetMethod,
         __DEFAULT_AGENT_NAME);
     DWORD dwLastError = httpClient.GetLastError();
     OutputDebugString(_T("DownloadURLToFile: ") + FormatErrorMessage(dwLastError));
 
     LPBYTE lpszHttpResponse = httpClient.QueryHTTPResponse();
     return (bResult ? S_OK : E_FAIL);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mihai MOGA
Architect Printec Group Romania SRL
Romania Romania
My professional background includes knowledge of analyst programmer for Microsoft Visual C++, Microsoft Visual C#, Microsoft Visual Basic, Sun Java, assembly for Intel 80x86 microprocessors, assembly for PIC microcontrollers (produced by Microchip Inc.), relational databases (MySQL, Oracle, SQL Server), concurrent version systems, bug tracking systems, web design (XHTML, CSS, XML, PHP/MySQL, JavaScript, Flash).
 
If you like my articles, please visit my website for more: http://www.mihaimoga.com/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralPlease Can you tell me how to show Upload and Download in gr... PinmemberMember 856969016-Jan-12 19:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 13 Jan 2012
Article Copyright 2012 by Mihai MOGA
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid