This is a Delphi class based on System.net.HTTPClient with the function of downloading a file from a URL and saving to the file destination:
function Download(const ASrcUrl : string; const ADestFileName : string): Boolean;
The main function is the ability to pause and resume partial downloads.
unit AcHTTPClient; interface uses System.Net.URLClient, System.net.HTTPClient; type TAcHTTPProgress = procedure(const Sender: TObject; AStartPosition : Int64; AEndPosition: Int64; AContentLength: Int64; AReadCount: Int64; ATimeStart : Int64; ATime : Int64; var Abort: Boolean) of object; TAcHTTPClient = class private FOnProgress: TAcHTTPProgress; FHTTPClient: THTTPClient; FTimeStart: cardinal; FCancelDownload: boolean; FStartPosition: Int64; FEndPosition: Int64; FContentLength: Int64; private procedure SetProxySettings(AProxySettings: TProxySettings); function GetProxySettings : TProxySettings; procedure OnReceiveDataEvent(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var Abort: Boolean); public constructor Create; destructor Destroy; override; property ProxySettings : TProxySettings read FProxySettings write SetProxySettings; property OnProgress : TAcHTTPProgress read FOnProgress write FOnProgress; property CancelDownload : boolean read FCancelDownload write FCancelDownload; function Download(const ASrcUrl : string; const ADestFileName : string): Boolean; end; implementation uses System.Classes, System.SysUtils, Winapi.Windows; constructor TAcHTTPClient.Create;
This, for example, is a form (this is just for testing the class):

unit WMain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, System.Math, AcHTTPClient, System.Net.URLClient; type TWinMain = class(TForm) BtnDownload: TButton; EdSrcUrl: TEdit; EdDestFilename: TEdit; ProgressBar: TProgressBar; BtnSospendi: TButton; LblInfo: TLabel; procedure BtnDownloadClick(Sender: TObject); procedure BtnCancelClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); private { Private declarations } FAcHTTPClient: TAcHTTPClient; FLastProcess: cardinal; procedure AcHTTPProgressEvent(const Sender: TObject; AStartPosition : Int64; AEndPosition: Int64; AContentLength: Int64; AReadCount: Int64; ATimeStart : Int64; ATime : Int64; var Abort: Boolean); public { Public declarations } end; var WinMain: TWinMain; implementation {$R *.dfm} procedure TWinMain.FormCreate(Sender: TObject); begin FLastProcess := GetTickCount; FAcHTTPClient := TAcHTTPClient.Create; FAcHTTPClient.OnProgress := AcHTTPProgressEvent; LblInfo.Caption := ''; ProgressBar.Max := 0; ProgressBar.Position := 0; end; procedure TWinMain.FormDestroy(Sender: TObject); begin FAcHTTPClient.Free; end; procedure TWinMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin FAcHTTPClient.CancelDownload := true; end; procedure TWinMain.BtnCancelClick(Sender: TObject); begin FAcHTTPClient.CancelDownload := true; end; procedure TWinMain.AcHTTPProgressEvent(const Sender: TObject; AStartPosition : Int64; AEndPosition: Int64; AContentLength: Int64; AReadCount: Int64; ATimeStart : Int64; ATime : Int64; var Abort: Boolean); function ConvertBytes(Bytes: Int64): string; const Description: Array [0 .. 8] of string = ('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); var i: Integer; begin i := 0; while Bytes > Power(1024, i + 1) do Inc(i); Result := FormatFloat('###0.##', Bytes / Power(1024, i)) +
I discovered the first problem, when Windows went into standby / hibernation state in the middle of boot, it broke the stream ... probably because windows also do not work on disk.
I have a partially fixed issue causing the system to remain active with SetThreadExecutionState this way:
procedure TWinMain.BtnDownloadClick(Sender: TObject); begin // Enable away mode and prevent the sleep idle time-out SetThreadExecutionState(ES_CONTINUOUS or ES_SYSTEM_REQUIRED); try try if FAcHTTPClient.Download(EdSrcUrl.Text, EdDestFilename.Text) then ShowMessage('File downloaded!'); except on E : Exception do ShowMessage(E.Message); end; finally // Clear EXECUTION_STATE flags to disable away mode // and allow the system to idle to sleep normally. SetThreadExecutionState(ES_CONTINUOUS); end; end;
however, the once downloaded file is still broken, and the problem seems to correlate with the resumption of partial download after the disk is idle.
offers?
side note: I'm on Delphi Berlin Update 2