procedure DownloadToStream(
const Url:
string; ms: TMemoryStream);
var
hSession : HINTERNET;
hService : HINTERNET;
lpBuffer :
array[0..1023]
of Byte;
dwBytesRead : DWORD;
dwBytesAvail : DWORD;
dwTimeOut : DWORD;
begin
hSession := InternetOpen('
sessionname', INTERNET_OPEN_TYPE_PRECONFIG,
nil,
nil, 0);
if not Assigned(hSession)
then Exit;
try
hService := InternetOpenUrl(hSession, PChar(
Url),
nil, 0, 0, 0);
if hService =
nil then
Exit;
try
dwTimeOut := 60000;
InternetSetOption(hService, INTERNET_OPTION_RECEIVE_TIMEOUT, @dwTimeOut, SizeOf(dwTimeOut));
if InternetQueryDataAvailable(hService, dwBytesAvail, 0, 0)
then
repeat
Application.ProcessMessages;
if not InternetReadFile(hService, @lpBuffer[0], SizeOf(lpBuffer), dwBytesRead)
then
Break;
if dwBytesRead <> 0
then
ms.WriteBuffer(lpBuffer[0], dwBytesRead);
until dwBytesRead = 0;
finally
InternetCloseHandle(hService);
end;
finally
InternetCloseHandle(hSession);
end;
end;