unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls, IdAntiFreezeBase, IdAntiFreeze;
type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
Button1: TButton;
Label1: TLabel;
IdAntiFreeze1: TIdAntiFreeze;
Label2: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
private
FFileSize: Integer;
FTimeInc: Integer;
FWorkCount: Integer;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
cURL = 'http://down.sandai.net/WebThunder1.12.2.210.exe';
var
s: TMemoryStream;
begin
s := TMemoryStream.Create;
try
with IdHTTP1 do begin
//允许链接跳转
HandleRedirects := True;
ReadTimeout := 100000;
//获得文件大小
Head(cURL);
FFileSize := Response.ContentLength;
FTimeInc := GetTickCount;
FWorkCount := 0;
//下载文件
Get(cURL,s);
s.SaveToFile('WebThunder1.12.2.210.exe');
end;
finally
s.Free;
end;
end;
procedure TForm1.IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
var
iNow,iDelay,iSize: Integer;
begin
iNow := GetTickCount;
//获得时间差
iDelay := iNow - FTimeInc;
if iDelay = 0 then Exit;
FTimeInc := iNow;
//获得时间差内下载的数据大小
iSize := AWorkCount - FWorkCount;
FWorkCount := AWorkCount;
Label1.Caption := IntToStr(AWorkCount div 1024) + '/' +
IntToStr(FFileSize div 1024) + ' KByte';
Label2.Caption := FloatToStr(iSize div iDelay) + ' Kb/秒';
end;
end.