我正在尝试使用WinINet库调用从http服务器下载文件。在我的本地Web服务器上,它运行得非常完美。但是当我尝试从互联网下载东西时,InternetReadFile仅读取任何文件(文本或二进制)的约10kb。下面的示例中TRANSFER_SIZE = 4096,因此有两个4kb的读取和一个2kb的读取。每次下一个InternetReadFile都会返回true和0字节读取。
hInternet = InternetOpen(L"Agent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD;
HINTERNET hSession = InternetOpenUrl(hInternet, URL, NULL, NULL, options, 0);
hFile = CreateFile(...);
...
do {
DWORD dwWriteSize, dwNumWritten;
BOOL bRead = InternetReadFile(hSession, pBuf, TRANSFER_SIZE, &dwReadSizeOut);
dwWriteSize = dwReadSizeOut;
if (bRead && dwReadSizeOut > 0) {
dwTotalReadSize += dwReadSizeOut;
WriteFile(hFile, pBuf, dwWriteSize, &dwNumWritten, NULL);
// File write error
if (dwWriteSize != dwNumWritten) {
CloseHandle(hFile);
return false;
}
}
else {
if (!bRead)
{
// Error
CloseHandle(hFile);
return false;
}
break;
}
} while(1);
如何使用WinINet库下载整个文件?