Here you go:
procedure TForm1.BGetClick(Sender: TObject);
var i:integer;
s:string;
begin
Progressbar1.Position := 0;
http := THTTPSend.Create;
http.Sock.OnStatus := @SocketGetCallBack;
if http.HTTPMethod('HEAD', ELink.Text) then
begin
// search for "Content-Length:" in header
for i:=0 to http.Headers.Count-1 do
begin
s:=UpperCase(http.Headers[i]);
if pos('CONTENT-LENGTH:',s)>0 then
TotalBytes := StrToIntDef(copy(s,pos(':',s)+1,length(s)),0);
end;
// get the file
http.Headers.Clear;
if http.HTTPMethod('GET', ELink.Text) then
http.Document.SaveToFile('Sbot_update.rar');
end;
http.Free;
end;
procedure TForm1.SocketGetCallBack(Sender: TObject; Reason: THookSocketReason;
const Value: string);
begin
if TotalBytes=0 then
exit;
case Reason of
HR_ReadCount:
begin
Inc(CurrentBytes, StrToIntDef(Value, 0));
ProgressBar1.Position := Round(1000 * (CurrentBytes / TotalBytes));
end;
HR_Connect: CurrentBytes := 0;
end;
end;
When "Content_length:" can't be found, TotalBytes =0 and SocketGetCallBack returns immediately. Also to avoid a divide by 0.