请教一个问题(关于下载http页面)
int downloadfile(char*url,char * buffer)
{
HINTERNET hSession;
HINTERNET hConnect;
try
{
hSession = InternetOpen(NULL,
PRE_CONFIG_INTERNET_ACCESS,NULL,NULL,0) ;
if(hSession==NULL) throw(1);
hConnect = InternetOpenUrl(hSession,
url, NULL,0,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD|INTERNET_FLA
G_DONT_CACHE, 0);
if(hConnect==NULL) throw(2);
}catch(...)
{
try
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
}catch(...){}
hSession=NULL;
hConnect=NULL;
return -1;
}
int curpos = 0;
char buf[BUFMAX + 4];
try
{
DWORD dwRequest;
DWORD dwRead;
dwRequest = BUFMAX;
while(true)
{
int ReadReturn = InternetReadFile(hConnect,(LPVOID)buf,dwRequest,&dwRead);
if(!ReadReturn)break;
if(dwRead==0)break;
memcpy((char*)buffer + curpos,buf,dwRead);
curpos += dwRead;
}
}catch(...){}
try
{
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
}catch(...){}
hSession=NULL;
hConnect=NULL;
return curpos;
}