IdHTTP Get下载网络文件时 经常卡死

chinawcs 2015-07-20 08:27:27
我是循环下载很多网络文件 ,但是下载过程中 会经常卡死不动 而且没有异常抛出 。主线程放IdAntiFreeze1 或者下载部分放 单独线程也会卡死。


IdHTTP 是阻塞的 有没有好的替代方案


var

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

begin
IdHTTP := TIdHttp.Create(nil);
IdHTTP.ProtocolVersion := pv1_1;
// IdHttp.RecvBufferSize := 1024 ;
IdHTTP.HandleRedirects := true;
IdHTTP.ConnectTimeout :=60000;
IdHTTP.ReadTimeout :=60000;
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.

lbl_error.Caption:='0';
tmr_dwonerror.Enabled :=true;
try
// 获取重定向后的下载地址和文件大小
IdHTTP.Head(FASrcURL);
FileSize := IdHTTP.Response.ContentLength;

if FileSize <= 0 then
exit;

{
DFileName := idhttp.URL.URI;
//由下载地址转换为保存文件名
while pos('/',DFileName) > 0 do
begin
delete(DFileName, 1, pos('/',DFileName));
end;
while pos('/',DFileName) > 0 do
begin
delete(DFileName, 1, pos('/',DFileName));
end;
if DFileName = '' then
exit;
}

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

cxProgressBar1.Properties.Max :=FileSize;

while FilePosition < FileSize do
begin
// 方法1:未验证
// IdHttp.Request.ContentRangeStart := FilePosition;
// IdHttp.Request.ContentRangeEnd := FilePosition + 1024;
// 方法2:已验证
try
IdHTTP.Request.Range := IntToStr(FilePosition) + '-';
if FilePosition + 10240 < 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
exit;
end;
// sleep(50);
end;
tmr_dwonerror.Enabled :=false;
Result := true;
finally
IdHTTP.Free;
MemStream.Free;
cxProgressBar1.Position:=0;
end;
...全文
356 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mit1208 2015-07-30
  • 打赏
  • 举报
回复
不行的话,采取多线程。
B525068254 2015-07-20
  • 打赏
  • 举报
回复


也是IdHTTP Get 自动升级用
wallace_xie 2015-07-20
  • 打赏
  • 举报
回复
ICS8也不错。
缘中人 2015-07-20
  • 打赏
  • 举报
回复
楼主用什么版本 D:\Users\Public\Documents\Embarcadero\Studio\16.0\Samples\Object Pascal\RTL\HttpDownload 这里有个官方的例子,多线程,挺不错的!
chinawcs 2015-07-20
  • 打赏
  • 举报
回复
我用的 xe2 + indy 10

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧