在获取网页内容,数据量如果比较大,可能会出现获取的内容错乱,此时可以通过停顿来使得缓存内容更新。
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <wininet.h>
#define MAXBLOCKSIZE 1024
#pragma comment (lib, "wininet.lib")
bool GetUrlInfo(const wchar_t* Url, std::string &rst)
{
bool isGet = false;
HINTERNET hSession = InternetOpen(L"RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET handle2 = InternetOpenUrl(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (handle2 != NULL)
{
char Temp[MAXBLOCKSIZE];
char TempChange[MAXBLOCKSIZE * 3] = { 0 };
ULONG Number = 1;
int step = 0;
while (Number > 0)
{
isGet = true;
InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);
int i = 0;
for (; i < Number; i++, step++)
{
TempChange[step] = Temp[i];
if (Temp[i] == '}')
{
TempChange[step + 1] = 0;
break;
}
}
rst += TempChange;
if (i + 1 < Number)
{
step = strlen(&Temp[i + 1]) - 10;
strcpy_s(TempChange, &Temp[i + 1]);
}
Sleep(10); //等待缓存数据
}
InternetCloseHandle(handle2);
handle2 = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
return isGet;
}