How can I copy files from http:// to the local hard disk in C++, MFC or a .bat file

Hello,

How can I copy files from a web location - http:// - to the local hard disk? The platforms I should use are C++, MFC with Visual Studio 2005 or a .bat file. I have found one tool, called httpcopy - http://www.activeplus.com/us/freeware/httpcopy/ - but I am not sure if I can distribute it with my product, that is not free, but payed. I prefer making this copy process in C++. Any ideas or sample code how this copy can be made?

Thanks,
Alex
Alex7777qqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaylanreilorCommented:
What do you think about this ?
http://www.codeproject.com/KB/IP/webgrab.aspx
0
alb66Commented:
You can use Win32 Internet Extensions (WinInet):
http://technet.microsoft.com/en-us/library/3w2azyt6(VS.80).aspx

Writing an Internet Client Application Using MFC WinInet Classes:
http://msdn2.microsoft.com/en-us/library/6f7sbkyc.aspx

This can aid you:
http://support.microsoft.com/?scid=kb%3Ben-us%3B149413&x=11&y=6
0
Alex7777qqAuthor Commented:
I have written the code below, based on the links given. It works, but I have the following problem. I want to copy all the files in the web address http://www.somedomain.net/, but I don't know their names. How can I use wildcards, or at least how can I diminish the timeout, so that when I try to download a file that does not exist, not to have a timeout after a too long period.
I am using
pSessionURL.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10);
to set the timeout, but it does not work. What I am doing wrong?
CStdioFile* pRemoteFile;
	FILE* pLocalFile;
	CInternetSession pSessionURL(L"URLSession");
	CString sURL;
		
	pRemoteFile = NULL;
	pLocalFile = NULL;
 
	pSessionURL.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10);
 
	pSessionURL.OpenURL(L"http://www.somedomain.net/somefile",1,NULL,NULL);
 
	pSessionURL.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 10);
	pSessionURL.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
	pRemoteFile = pSessionURL.OpenURL(L"http://www.somedomain.net/somefile",1,INTERNET_FLAG_TRANSFER_BINARY, NULL,NULL);
	
	if (pRemoteFile != NULL)
	{
 
	if (( pLocalFile = fopen("localfilename" ,"wb")) != NULL) 
	{
		UINT dwActual = pRemoteFile->SeekToEnd();
 
		//calculate size of buffer based on whole file size
		char* pBuffer = (char*)calloc(sizeof(char),(dwActual));
 
		pRemoteFile->SeekToBegin();
		UINT lNumBytesRead = pRemoteFile->Read(pBuffer,dwActual);
 
		fwrite(pBuffer,dwActual,1,pLocalFile);
	}
 
	fclose(pLocalFile);
	delete pRemoteFile;
	}
	pSessionURL.Close();

Open in new window

0
kaylanreilorCommented:
Since your trying to manipulate file like that you should have a look to the CFtpConnection class or, better, re-read the article about the FTP Client application.
http://msdn2.microsoft.com/en-us/library/hf9x9wb4(VS.80).aspx
0
alb66Commented:
Unfortunatly there is a bug in Windows: SetOption does not set timeout values.

See Microsoft knowledge base at:
http://support.microsoft.com/kb/176420/EN-US

and a possible work around at:
http://support.microsoft.com/?scid=kb%3Ben-us%3B224318&x=11&y=17

I used it in an application of mine and works well.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.