Reputation: 6772
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.
Upvotes: 2
Views: 2699
Reputation: 43043
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.
Upvotes: 4
Reputation: 2797
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.
Upvotes: 0