用 Delphi 下载网络文件

procedureTForm1.Button2Click(Sender:TObject);
var
MyStream:TMemoryStream;
begin
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
MyStream:=TMemoryStream.Create;
try
//下载我站点的一个ZIP文件
IdHTTP1.Get('http://www.138soft.com/download/Mp3ToExe.zip',MyStream);
except//INDY控件一般要使用这种try..except结构.
Showmessage('网络出错!');
MyStream.Free;
Exit;
end;
MyStream.SaveToFile('c:\Mp3ToExe.zip');
MyStream.Free;
Showmessage('OK');
end;

procedureTForm1.IdHTTP1WorkBegin(Sender:TObject;AWorkMode:TWorkMode;
constAWorkCountMax:Integer);
begin
ProgressBar1.Max:=AWorkCountMax;
ProgressBar1.Min:=0;
ProgressBar1.Position:=0;
end;

IdHTTP1的Get还有一种形式就是获取字符串:例如,上面的程序可以改写成:
procedureTForm1.Button1Click(Sender:TObject);
var
MyStr:String;
begin
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
try
MyStr:=IdHTTP1.Get('http://www.138soft.com/default.htm');
except
Showmessage('网络出错!');
Exit;
end;
Showmessage(MyStr);
end;

你可能感兴趣的:(C++,c,C#,Delphi)