// https://stackoverflow.com/questions/13638778/delphi-downloading-a-file-using-wininet
function DownloadFile(
URL: widestring; Path:
string): Boolean;
const
BLOCK_SIZE = 1024;
var
InetHandle: Pointer;
URLHandle: Pointer;
FileHandle: Cardinal;
BytesRead: Cardinal;
DownloadBuffer: Pointer;
Buffer:
array [1 .. BLOCK_SIZE]
of byte;
BytesWritten: Cardinal;
begin
Result := False;
InetHandle := InternetOpenW(PWideChar(
URL), 0, 0, 0, 0);
if not Assigned(InetHandle)
then RaiseLastOSError;
try
InternetConnect (
InetHandle,
PChar(
URL),
INTERNET_DEFAULT_HTTPS_PORT,
PChar('
'),
// User
PChar('
'),
// Pass
INTERNET_SERVICE_HTTP,
0,
0
);
// auch nil,nil für User und Pass klappen nicht
URLHandle := InternetOpenUrlW(InetHandle, PWideChar(
URL), 0, 0, 0, 0);
if not Assigned(URLHandle)
then RaiseLastOSError;
// hier kommt der Fehler
try
FileHandle := CreateFileW(PWideChar(Path), GENERIC_WRITE, FILE_SHARE_WRITE, 0,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
if FileHandle = INVALID_HANDLE_VALUE
then RaiseLastOSError;
try
DownloadBuffer := @Buffer;
repeat
if (
not InternetReadFile(URLHandle, DownloadBuffer, BLOCK_SIZE, BytesRead))
or (
not WriteFile(FileHandle, DownloadBuffer^, BytesRead, BytesWritten, 0))
then
RaiseLastOSError;
until BytesRead = 0;
Result:= True;
finally
CloseHandle(FileHandle);
end;
finally
InternetCloseHandle(URLHandle);
end;
finally
InternetCloseHandle(InetHandle);
end;
end;