Link to home
Start Free TrialLog in
Avatar of Alex7777qq
Alex7777qq

asked on

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
SOLUTION
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex7777qq
Alex7777qq

ASKER

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

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial