用HTTP从网站下载文件

方法一,用IdHttp:
procedure TForm1.Button1Click(Sender: TObject);
var
fs: TFileStream;
begin
fs := TFileStream.Create('c:\aaa.htm', fmCreate);
IdHTTP1.Get('http://www.abc.com/aaa.shtml', fs);
fs.Free;
end;

方法二,用UrlMon:
uses  
UrlMon


function DownloadFile(Source, Dest: string): Boolean;
begin
try
  Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
except
  Result := False;
end;
end;

 
procedure TForm1.Button1Click(Sender: TObject);
begin
if DownloadFile(edit1.Text, edit2.Text) then
  ShowMessage('下载成功')
else ShowMessage('下载失败');
end;

Last modification:August 16, 2009
如果觉得我的文章对你有用,请随意赞赏