2010-02-02 14 views

risposta

20

ho codificato questo esempio, utilizzando un solo HTTP GET, con Indy 10, spero che funziona con Indy 9 troppo:

uses 
    {...} IdHTTP, IdComponent; 

type 
    TFormMain = class(TForm) 
    {...} 
    private 
    {...} 
    procedure HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64); 
    end; 
{...} 

procedure TFormMain.Button1Click(Sender: TObject); 
var 
    Http: TIdHTTP; 
    MS: TMemoryStream; 
begin 
    Http := TIdHTTP.Create(nil); 
    try 
    MS := TMemoryStream.Create; 
    try 
     Http.OnWork:= HttpWork; 

     Http.Get('http://live.sysinternals.com/ADExplorer.exe', MS); 
     MS.SaveToFile('C:\ADExplorer.exe'); 

    finally 
     MS.Free; 
    end; 
    finally 
    Http.Free; 
    end; 
end; 

procedure TFormMain.HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64); 
var 
    Http: TIdHTTP; 
    ContentLength: Int64; 
    Percent: Integer; 
begin 
    Http := TIdHTTP(ASender); 
    ContentLength := Http.Response.ContentLength; 

    if (Pos('chunked', LowerCase(Http.Response.TransferEncoding)) = 0) and 
    (ContentLength > 0) then 
    begin 
    Percent := 100*AWorkCount div ContentLength; 

    MemoOutput.Lines.Add(IntToStr(Percent)); 
    end; 
end; 
+2

Il valore Response.ContentLength non è sempre valida. In particolare, nelle risposte HTTP 1.1 che utilizzano la codifica di trasferimento "chunked", l'intestazione "Content-Length" non può essere utilizzata. Durante i trasferimenti Chunked, la dimensione totale dei dati non è nota in anticipo, poiché i dati vengono trasmessi in più blocchi e ogni blocco ha le proprie dimensioni internamente. –

+1

Meglio? Ora utilizzo esattamente le stesse condizioni di 'TIdCustomHTTP.ReadResult()' all'interno dell'unità 'IdHTTP.pas' – ulrichb

+2

e non dimentico di scrivere' Application.ProcessMessages(); 'su evento OnWork! –

Problemi correlati