Click here to Skip to main content
11,579,360 members (73,770 online)
Click here to Skip to main content

A class to download files from the net

, 16 Apr 2002 CPOL 322.7K 7.1K 114
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Comments and Discussions

 
QuestionDownloaded file corrupt in VS2013 - UNICODE problem? Pin
renninha18-Jun-14 3:51
memberrenninha18-Jun-14 3:51 
QuestionPOST instead of GET Pin
.dan.g.5-Feb-12 17:27
member.dan.g.5-Feb-12 17:27 
QuestionProblem with files Pin
vantomas12-Aug-11 4:08
membervantomas12-Aug-11 4:08 
Generalsame code in "pure c++" (non-MFC) Pin
mimosa10-Jun-11 21:27
membermimosa10-Jun-11 21:27 
QuestionHow to grab an html file generated with embebded javascript? Pin
mimosa7-Aug-10 10:56
membermimosa7-Aug-10 10:56 
GeneralWhere are downloaded files Pin
nicolla100331-Jul-10 7:35
membernicolla100331-Jul-10 7:35 
Generaltype files Pin
barhouming9-Jul-10 0:54
memberbarhouming9-Jul-10 0:54 
QuestionHow can I write to the net? Pin
DanYELL14-Feb-09 15:32
memberDanYELL14-Feb-09 15:32 
GeneralQuestion about WebGrab... Pin
LilMoke25-Nov-08 5:13
memberLilMoke25-Nov-08 5:13 
GeneralWebGrab with SSL and Invalid Certificate Pin
Doug Knudson15-Oct-07 3:15
memberDoug Knudson15-Oct-07 3:15 
GeneralRe: WebGrab with SSL and Invalid Certificate Pin
Lery7817-Apr-08 11:59
memberLery7817-Apr-08 11:59 
GeneralProblems [modified] Pin
Atridas16-Feb-07 9:09
memberAtridas16-Feb-07 9:09 
GeneralRe: Problems Pin
effem22-Feb-07 0:57
membereffem22-Feb-07 0:57 
Questionproxy ? Pin
effem16-Feb-07 1:36
membereffem16-Feb-07 1:36 
AnswerRe: proxy ? Pin
A duley31-Aug-07 7:02
memberA duley31-Aug-07 7:02 
GeneralHTTP Authentication Pin
Priyank Bolia3-Mar-06 2:47
memberPriyank Bolia3-Mar-06 2:47 
GeneralBUG: Unicode is not supported, wrong buffer size Pin
Alex_Raider28-Feb-06 22:19
memberAlex_Raider28-Feb-06 22:19 
GeneralRe: BUG: Unicode is not supported, wrong buffer size Pin
Sandro7819-Jun-08 4:01
memberSandro7819-Jun-08 4:01 
GeneralProxy Not working Pin
srivathsan.a9-Jan-06 20:45
membersrivathsan.a9-Jan-06 20:45 
Generalhttps Pin
Orxorand21-Nov-05 0:50
memberOrxorand21-Nov-05 0:50 
GeneralAnother buggie Pin
dchris_med29-Oct-05 11:06
memberdchris_med29-Oct-05 11:06 
GeneralConnect Timout Failed !!! Hung my machine ! Pin
sonawane ajay23-Mar-05 0:36
membersonawane ajay23-Mar-05 0:36 
GeneralProblem with setting timeout Pin
PetoG15-Dec-04 0:02
memberPetoG15-Dec-04 0:02 
Generaluse in pocketPC Pin
chupas228-Nov-04 23:32
memberchupas228-Nov-04 23:32 
GeneralA Bug and and how to fix it! Pin
Ehsan-de-Burge28-Aug-04 20:50
memberEhsan-de-Burge28-Aug-04 20:50 
GeneralCookies Pin
Orxorand4-Apr-04 23:40
memberOrxorand4-Apr-04 23:40 
GeneralCInternetSession Pin
FerrariDMan13-Jan-04 4:09
memberFerrariDMan13-Jan-04 4:09 
GeneralGets file through proxy ... Pin
Eugene Pustovoyt12-Jan-04 2:08
memberEugene Pustovoyt12-Jan-04 2:08 
GeneralRe: Gets file through proxy ... Pin
Orxorand21-Nov-05 0:31
memberOrxorand21-Nov-05 0:31 
GeneralBroken Downloads Pin
angello617-Dec-03 0:03
memberangello617-Dec-03 0:03 
GeneralSimultaneous download in multithreading Pin
mimosa23-Sep-03 12:28
membermimosa23-Sep-03 12:28 
GeneralRe: Simultaneous download in multithreading Pin
mimosa8-May-06 18:22
membermimosa8-May-06 18:22 
GeneralRe: Simultaneous download in multithreading Pin
tic8419-Oct-07 0:04
membertic8419-Oct-07 0:04 
QuestionHow can I save to file? Pin
Karl Haupt15-Jul-03 2:25
memberKarl Haupt15-Jul-03 2:25 
GeneralProxy Authentication Pin
phykell16-Mar-03 23:58
memberphykell16-Mar-03 23:58 
GeneralRe: Proxy Authentication Pin
phykell21-Mar-03 22:32
memberphykell21-Mar-03 22:32 
GeneralRe: Proxy Authentication Pin
Eugene Pustovoyt11-Jan-04 22:37
memberEugene Pustovoyt11-Jan-04 22:37 
GeneralRe: Proxy Authentication Pin
phykell11-Jan-04 23:45
memberphykell11-Jan-04 23:45 
GeneralUnused GlobalAlloc'd memory... Pin
Nathan Lewis4-Jan-03 13:26
memberNathan Lewis4-Jan-03 13:26 
QuestionWhere is INTERNET_OPEN_FLAG_USE_EXISTING_CONNECT ? Pin
Anonymous14-Oct-02 11:22
sussAnonymous14-Oct-02 11:22 
AnswerRe: Where is INTERNET_OPEN_FLAG_USE_EXISTING_CONNECT ? Pin
anomymous15-Dec-03 19:10
sussanomymous15-Dec-03 19:10 
GeneralNot implement yet Pin
Anonymous14-Oct-02 6:49
sussAnonymous14-Oct-02 6:49 
GeneralIt'd be so much nicer if... Pin
Bin24-Jul-02 19:50
memberBin24-Jul-02 19:50 
GeneralLeech Files Pin
Vikash Dubey23-Jul-02 0:24
sussVikash Dubey23-Jul-02 0:24 
Generalhalt Pin
bryce14-Jun-02 13:23
memberbryce14-Jun-02 13:23 
Generalproblems with SetProxy() Pin
Davide Pizzolato13-May-02 20:33
memberDavide Pizzolato13-May-02 20:33 
QuestionHow to get all files of a page? Pin
Anonymous28-Apr-02 13:42
memberAnonymous28-Apr-02 13:42 
AnswerRe: How to get all files of a page? Pin
bryce28-Apr-02 13:55
memberbryce28-Apr-02 13:55 
GeneralRe: How to get all files of a page? Pin
Ravi Bhavnani28-Apr-02 18:41
memberRavi Bhavnani28-Apr-02 18:41 
AnswerRe: How to get all files of a page? Pin
Ravi Bhavnani28-Apr-02 18:43
memberRavi Bhavnani28-Apr-02 18:43 

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 | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 17 Apr 2002
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid