I wrote some code to download images for a test application before, so I can post the main function here which does most of the work. Keep in mind this code is Windows specific, so it won't do you much good if you are using other platforms.
Code:
int Download(char* szFile,char* szLink)
{
HINTERNET hInternet,hURL;
char contentLength[1000];
DWORD fileSize;
unsigned long clSize = 1000;
int totalTransferred = 0;
unsigned char *buff;
HANDLE hFileWrite;
const int readBuffSize = 5000;
hInternet = InternetOpen("app",INTERNET_OPEN_TYPE_DIRECT,0,0,0);
if (!hInternet)
return 0;
hURL = InternetOpenUrl(hInternet,szLink,0,0,INTERNET_FLAG_NO_CACHE_WRITE,0);
if (!hURL)
{
InternetCloseHandle(hInternet);
return 0;
}
if (!HttpQueryInfo(hURL,HTTP_QUERY_CONTENT_LENGTH,contentLength,&clSize,0))
{
MessageBox(NULL,"Unable to get content length","Error",0);
InternetCloseHandle(hInternet);
return 0;
}
else
fileSize = atoi(contentLength);
buff = malloc(readBuffSize);
/* Open the file to write the contents */
hFileWrite = CreateFile(szFile,GENERIC_WRITE,0,0,OPEN_ALWAYS,0,0);
for (;;)
{
DWORD n;
DWORD dwBytesWritten = 0;
if(!InternetReadFile(hURL,buff,readBuffSize,&n))
{
MessageBox(NULL,"There was an error attemtping to download file.","ERROR",0);
break;
}
if (n == 0)
{
/* EOF */
break;
}
totalTransferred += n;
WriteFile(hFileWrite,buff,n,&dwBytesWritten,0);
}
CloseHandle(hFileWrite);
free(buff);
InternetCloseHandle(hInternet);
return 1;
}