2

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.

Gad D Lord
  • 6,620
  • 12
  • 60
  • 106

2 Answers2

4

For such buffers, I usualy code:

var
  Buf: array[word] of byte;

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:

Microsoft Windows HTTP Services (WinHTTP) provides developers with a server-supported, high-level interface to the HTTP/1.1 Internet protocol. WinHTTP is designed to be used primarily in server-based scenarios by server applications that communicate with HTTP servers.

WinINet was designed as an HTTP client platform for interactive desktop applications, such as Microsoft Internet Explorer, Microsoft Office, and Microsoft Money. WinINet displays a user interface for some operations such as collecting user credentials. WinHTTP, however, handles these operations programmatically. Server applications that require HTTP client services should use WinHTTP instead of WinINet. For more information, see Porting WinINet Applications to WinHTTP.

WinHTTP is also designed for use in system services and HTTP-based client applications. However, single-user applications that require FTP protocol functionality, cookie persistence, caching, automatic credential dialog handling, Internet Explorer compatibility, or downlevel platform support should consider using WinINet.

Extracted from MSDN

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.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • Link is broken! – Gabriel Feb 10 '22 at 19:31
  • updated link: https://blog.synopse.info/?post/2011/07/04/WinINet-vs-WinHTTP – Gabriel Feb 10 '22 at 19:33
  • Can you please confirm that the function that implements the WinHTTP function (for downloading a file, for example) is TSQLHttpClientWinSock.InternalRequest ? Thanks. It would be nice if the function could be somehow separated in a single file.... – Gabriel Feb 10 '22 at 19:46
  • Update: I just discovered that the Indy "download file" function (IdHTTP.pas) is a much better separated. – Gabriel Feb 10 '22 at 20:01
0

There is no significant difference, but i think that best value is 65 536 bytes (tcp limit for package), or 30 000 bytes because your smallest files less of 65 536.

TheHorse
  • 2,787
  • 1
  • 23
  • 32
  • 1
    Agreed, programmers tend to like to pull powers of 2 out of their hats. Given the large size of disk caches, I usually go for 128K or 256K, but unless you actually test it with a local http server, and a code profiler, it's likely impossible to tell the difference. – Warren P Jul 03 '11 at 21:48