Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

http download

Status
Not open for further replies.

huricane

Programmer
Dec 28, 2002
6
0
0
GB
I am new to C++ and I need help with downloading a file from a webserver unto the local machine

Steps

1. first I need to connect to an asp page passing the machine IP as an argument eg 2. In response the asp page will return the path to the file to download this page is already done
3. The C++ application then proceeds to download the file unto the local machine
4. After downloading it proceeds to load the a VB.exe application

Any help will be appreciated
 
Hello Hurricane,

Here's some code that'll download a file from a given url and save it to local hard disk. You'll need to #include the approriate header files.

[tt]
unsigned char buffer[512];
int bytes = 1;
int totalRead = 0;
CString fileName = "";
CString query = "";
CString
Code:
url = "[URL unfurl="true"]http://some_url.com";[/URL]
CHttpFile* file = NULL;
CInternetSession* inetSession = NULL;

try{

inetSession = new CInternetSession("AppName",1,
INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);

file = (CHttpFile*)inetSession->OpenURL(LPCTSTR(url),
1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);

// get size of remote file
file->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,query);

fileName = file->GetFileURL();
fileName.Delete(0,fileName.ReverseFind('/')+1);


}
catch(CInternetException* e) {
e->ReportError();
e->Delete();
// abort here!
}
catch(CFileException* e) {
e->ReportError();
e->Delete();
// abort here!
}

FILE* f = fopen(LPCTSTR(fileName),"wb+");
while(bytes>0 && totalRead<atol(query))
{
bytes = file->Read(buffer,512);
fwrite(buffer,bytes,1,f);
totalRead+=bytes;
}
fflush(f);
fclose(f);
file->Close();
[/tt]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Hello again Hurricane, something I forgot to include but assumed you'd know it - don't forget to 'delete' the CInternetSession object [tt]inetSession[/tt] once you're finished with it:

[tt]delete inetSession;[/tt]

to avoid a memory leak!

The headers you need to include are:

[tt]#include <afxinet.h>[/tt]

You obviously swap the values of the URL and &quot;AppName&quot; strings for whatever values you see fit. (The AppName is the string that is passed to the URL as the ENV value HTTP_USER_AGENT - or whatever it is!).

If you put this code in a separate thread, you can implement a progress bar or whatever within the file-writing loop to show the user the progress of download. Simply set the progress bar's range from 0 to atol(query) and then move the position of the bar according to the number of bytes read! Works real cool!! :)

Sorry I ommitted this extra info earlier - I was in a rush and had to leave!! [auto]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top