Powered byCoderTown
OpenURL sample
This sample function allows to copy a file from any location (remote or not) to local file. For url parameter valid values are
http://visualcpp.net/text.txt,C:\test.doc,ftp://user:password@ftp.any.com/folder/file.ext etc.
// #include "afxinet.h"
BOOL getURLFile(const char *url, const char *filename, CString &errorMessage) {
const int FILEBUFLEN = 1024;
char httpBuff[FILEBUFLEN];
TCHAR szCause[255];
errorMessage = "OK";
TRY {
CInternetSession session;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 3);
CFile *remoteFile = session.OpenURL(url, 1 ,
INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
CFile localFile(filename, CFile::modeCreate |
CFile::modeWrite |
CFile::typeBinary);
int numBytes;
while (numBytes = remoteFile->Read(httpBuff, FILEBUFLEN)) {
localFile.Write(httpBuff, numBytes);
}
}
CATCH_ALL(error) {
error->GetErrorMessage(szCause,254,NULL);
errorMessage.Format("%s",szCause);
return FALSE;
}
END_CATCH_ALL;
return TRUE;
}