Click here to Skip to main content
15,885,141 members
Articles / Desktop Programming / MFC
Article

A class to download files from the net

Rate me:
Please Sign up or sign in to vote.
4.90/5 (30 votes)
16 Apr 2002CPOL 420.1K   8.7K   118   89
A simple class to ease the task of downloading files from the net

Sample Image - WebGrab.gif

CWebGrab is a simple class that allows you to quickly and easily download files from the net. The class is merely a light wrapper for the MFC internet classes, and as such supports any protocol that the MFC classes support.

The class is extremely simple to use and has only one main function that you need to call: CWebGrab::GetFile. The syntax is:

BOOL GetFile(LPCTSTR szURL, CString& strBuffer, 
             LPCTSTR szAgentName = NULL, CWnd* pWnd = NULL);

Downloads a file from the given URL

  • szURL is the URL
  • strBuffer is a CString buffer that will hold the contents of the downloaded file
  • szAgentName is the agent name you want to send to the website (may be NULL)
  • pWnd is a status window. Status updates will be sent to this window using SetWindowText()
BOOL GetFileInfo(LPCTSTR  szURL, CString& strLastModified, DWORD& dwSize,
                 DWORD& dwServerError, LPCTSTR szAgentName = NULL, 
                 CWnd* pWnd = NULL);

(Added by Ravi Bhavnani) Retrieves the information about a file from the given URL

  • szURL is the URL
  • strLastModified is a CString buffer that will hold the last modified date of the file
  • dwServerError will return any error codes that were raised.
  • szAgentName is the agent name you want to send to the website (may be NULL)
  • pWnd is a status window. Status updates will be sent to this window using SetWindowText()

Bryce Burrows kindly updated the code to include the following functions:

void SetUseProxy(bool use);

Sets whether or not to use a proxy

void SetProxyServer(LPCSTR server);

Sets the proxy server

void SetProxyPort(UINT port);

Sets the proxy port

void SetProxy(LPCSTR proxy, WORD port, bool use=true);

Sets the proxy information in one call

void SetForceReload (bool bForceReload)

Sets whether or not to force a reload when downloading a document. If a force reload is set the the document will not be a cached version.

void GetForceReload()

Gets whether or not to force a reload when downloading a document.

void SetTimeOut(DWORD timeOut);

Sets the timeout for the operation

double GetRate();

Retrieves the download rate in Kb/sec

SHORT GetErrorCode();
CString GetErrorMessage();

Get the error code and message (if any)

Example of use

#include "webgrab.h"

void CWebGrabberDlg::OnDownload() 
{
    CString strBuffer;
    CWebGrab grab;
    if (grab.GetFile("http://www.mysite.com/index.htm", // the url
                     strBuffer,                         // buffer for data
                     _T("WebGrab demo"),                // agent name
                     GetDlgItem(IDC_STATUS)))           // Plain ol' CStatic window
    {
        TRACE0("everything went OK\n")
    }
}

History

26 Jan 2002 - Bryce Burrows kindly added the proxy code.

16 Feb 2002 - Bryce Burrows source code update.

2 Mar 2002 - Ravi Bhavnani added a "force reload" option to ensure that the downloaded data is not read from the cache, plus Get/SetForceReload() functions.

17 Apr 2002 - Ravi Bhavnani added a GetFileInfo

License

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


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralA Bug and and how to fix it! Pin
Ehsan Baghaki28-Aug-04 20:50
Ehsan Baghaki28-Aug-04 20:50 
GeneralCookies Pin
Orxorand4-Apr-04 23:40
Orxorand4-Apr-04 23:40 
GeneralCInternetSession Pin
FerrariDMan13-Jan-04 4:09
FerrariDMan13-Jan-04 4:09 
GeneralGets file through proxy ... Pin
Eugene Pustovoyt12-Jan-04 2:08
Eugene Pustovoyt12-Jan-04 2:08 
GeneralRe: Gets file through proxy ... Pin
Orxorand21-Nov-05 0:31
Orxorand21-Nov-05 0:31 
GeneralBroken Downloads Pin
angello617-Dec-03 0:03
angello617-Dec-03 0:03 
GeneralSimultaneous download in multithreading Pin
mimosa23-Sep-03 12:28
mimosa23-Sep-03 12:28 
GeneralRe: Simultaneous download in multithreading Pin
mimosa8-May-06 18:22
mimosa8-May-06 18:22 
GeneralRe: Simultaneous download in multithreading Pin
tic8419-Oct-07 0:04
tic8419-Oct-07 0:04 
QuestionHow can I save to file? Pin
Karl Haupt15-Jul-03 2:25
Karl Haupt15-Jul-03 2:25 
GeneralProxy Authentication Pin
thowra16-Mar-03 23:58
thowra16-Mar-03 23:58 
GeneralRe: Proxy Authentication Pin
thowra21-Mar-03 22:32
thowra21-Mar-03 22:32 
GeneralRe: Proxy Authentication Pin
Eugene Pustovoyt11-Jan-04 22:37
Eugene Pustovoyt11-Jan-04 22:37 
GeneralRe: Proxy Authentication Pin
thowra11-Jan-04 23:45
thowra11-Jan-04 23:45 
GeneralUnused GlobalAlloc'd memory... Pin
Nathan Lewis4-Jan-03 13:26
Nathan Lewis4-Jan-03 13:26 
QuestionWhere is INTERNET_OPEN_FLAG_USE_EXISTING_CONNECT ? Pin
Anonymous14-Oct-02 11:22
Anonymous14-Oct-02 11:22 
AnswerRe: Where is INTERNET_OPEN_FLAG_USE_EXISTING_CONNECT ? Pin
rr1215-Dec-03 19:10
rr1215-Dec-03 19:10 
GeneralNot implement yet Pin
Anonymous14-Oct-02 6:49
Anonymous14-Oct-02 6:49 
GeneralIt'd be so much nicer if... Pin
Abin24-Jul-02 19:50
Abin24-Jul-02 19:50 
GeneralLeech Files Pin
Vikash Dubey23-Jul-02 0:24
Vikash Dubey23-Jul-02 0:24 
Generalhalt Pin
bryce14-Jun-02 13:23
bryce14-Jun-02 13:23 
Generalproblems with SetProxy() Pin
Davide Pizzolato13-May-02 20:33
Davide Pizzolato13-May-02 20:33 
QuestionHow to get all files of a page? Pin
28-Apr-02 13:42
suss28-Apr-02 13:42 
AnswerRe: How to get all files of a page? Pin
bryce28-Apr-02 13:55
bryce28-Apr-02 13:55 
GeneralRe: How to get all files of a page? Pin
Ravi Bhavnani28-Apr-02 18:41
professionalRavi Bhavnani28-Apr-02 18:41 

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

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