Home > Back-end >  Often stuck when IdHTTP Get download network file
Often stuck when IdHTTP Get download network file

Time:09-26

I download a lot of network file is circular, but often card in the process of downloading death and no exception is thrown, the main thread put IdAntiFreeze1 or download card be separate thread will die,


IdHTTP is blocking any good alternative

 
Var

IdHTTP: TIdHttp;
MemStream: TMemoryStream;
UrlStr, DFileName: string;
FilePosition: int64;
FileSize: Integer;

The begin
IdHTTP:=TIdHttp. Create (nil);
IdHTTP. ProtocolVersion:=pv1_1;
//IdHttp. RecvBufferSize:=1024;
IdHTTP. HandleRedirects:=true;
IdHTTP. ConnectTimeout:=60000;
IdHTTP. ReadTimeout:=60000;
IdAntiFreeze1. OnlyWhenIdle:=False;//Settings make the program.

Lbl_error. Caption:='0';
Tmr_dwonerror. Enabled:=true;
Try
//get the redirection after download address and file size
IdHTTP. Head (FASrcURL);
FileSize:=IdHTTP. Response. ContentLength;

If FileSize & lt;=0 then
The exit;

{
DFileName:=idhttp. URL. URI;
//by downloading address translation for the save file name
While pos ('/', DFileName) & gt; 0 do
The begin
Delete (DFileName, 1, pos ('/', DFileName));
end;
While pos ('/', DFileName) & gt; 0 do
The begin
Delete (DFileName, 1, pos ('/', DFileName));
end;
If DFileName="' then
The exit;
}

FilePosition:=0;
MemStream:=TMemoryStream. Create;

CxProgressBar1. Properties. Max:=FileSize;

While FilePosition & lt; The FileSize do
The begin
//method 1: not verify
//IdHttp. Request. ContentRangeStart:=FilePosition;
//IdHttp. Request. ContentRangeEnd:=FilePosition + 1024;
//method 2: verified
Try
IdHTTP. Request. Range:=IntToStr (FilePosition) + '-';
If FilePosition + 10240 & lt; The FileSize then
IdHTTP. Request. Range:=IdHTTP. Request. Range +
IntToStr (FilePosition + 10239);

IdHTTP. Get (IdHTTP. URL. URI, MemStream);
//MemStream. LoadFromStream (IdHttp. Response. ContentStream);
MemStream. SaveToFile (FASaveFileName);

FilePosition:=MemStream. Size;
CxProgressBar1. Position:=FilePosition;
Application. ProcessMessages;
Except,
The exit;
end;
//sleep (50);
end;
Tmr_dwonerror. Enabled:=false;
Result:=true;
The finally
IdHTTP. Free;
MemStream. Free;
CxProgressBar1. Position:=0;
end;

CodePudding user response:

The building Lord what version
D: \ Users \ Public \ Documents \ Embarcadero \ Studio \ 16.0 \ Samples \ Object Pascal \ RTL \ HttpDownload
Here is an official example, multithreading, quite good!

CodePudding user response:

http://docwiki.embarcadero.com/CodeExamples/XE8/en/RTL.HttpDownload_Sample

CodePudding user response:

ICS8 is good also,

CodePudding user response:



Also IdHTTP Get automatic updates

CodePudding user response:

I use the indy xe2 + 10

CodePudding user response:

If not, take a multi-threaded,