I use WinInet.h in Delphi to download files over HTTP with the average size between 30 KB and 1.5 MB.
var
Buf: array[0..BUFFER_SIZE - 1] of Byte;
while BOOL(InternetReadFile(hUrl, @Buf, SizeOf(Buf), BytesRead)) and (BytesRead > 0) do
if Terminated then
Exit
else
begin
FStream.WriteBuffer(Buf, BytesRead);
Synchronize(UpdateProgress);
FillChar(Buf, SizeOf(Buf), 0);
end;
What is the recommended buffer size for such downloads – if shouldn’t be too big neither too small.
For such buffers, I usualy code:
Which allocates 64 KB of buffer.
But, from my little experiment, WinINet is so slow that the internal buffer size won’t change much.
If you look for performance, take a look at WinHTTP, which is much faster than WinINet. More than 10 times faster, at least for multiple connections. Only missing feature is the dialog boxes for remote dial-up access:
I’ve implemented both WinInet and WinHTTP client access in our Open Source ORM framework. You may take a look at this blog article to find out more info about WinHTTP.
As far as I know, the latest version of IE uses WinHTTP instead of WinINet. So we may consider going in the same direction.