I need to download a file but it could happen that the sender doesn't send the data smoothly. It could happen that after a few megabytes there is a period in which no data is sent. After approx. 30 to 180 seconds the sender resumes providing data for the downloader.
I attached the function that I use to download the data. Unfortunately, this function returns when there is a delay on the sender's side and consequently the receiver gets an incomplete file.
If I could specify a timeout that the InternetReadFile would allow to wait for the data stream to continue that would solve my problem.
If you have a different solution let me know. I prefer using WinInet since our software runs in environments where network configuration is done through Internet Explorer settings.
function THttpManager.DownloadFile(const aUrl: string; const pFileName: String): Boolean;
lpBuffer: array[0..1024 + 1] of Char;
Result := False;
hSession := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(hSession) then
hService := InternetOpenUrl(hSession, PChar(aUrl), nil, 0, 0, 0);
if Assigned(hService) then
lFileStream := TFileStream.Create( pFileName, fmCreate );
while True do
dwBytesRead := 1024;
InternetReadFile(hService, @lpBuffer, 1024, dwBytesRead);
if dwBytesRead = 0 then break;
lpBuffer[dwBytesRead] := #0;
lFileStream.WriteBuffer( lpBuffer, dwBytesRead );
Result := True;