这里用的是Winet里面的API,没有用MFC的,代码如下:
#define MAXBLOCKSIZE 1024
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
void GetWebSrcCode(const char *Url);
int _tmain(int argc, _TCHAR* argv[])
{
GetWebSrcCode("http://www.csdn.net/");
return 0;
}
void GetWebSrcCode(const char *Url)
{
HINTERNET hSession = InternetOpen("zwt", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET hURL = InternetOpenUrl(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (hURL != NULL)
{
char Temp[MAXBLOCKSIZE] = {0};
ULONG Number = 1;
FILE *stream;
if( (stream = fopen( "E:\\test.html", "wb" )) != NULL )
{
while (Number > 0)
{
InternetReadFile(hURL, Temp, MAXBLOCKSIZE - 1, &Number);
fwrite(Temp, sizeof (char), Number , stream);
}
fclose( stream );
}
InternetCloseHandle(hURL);
hURL = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
}