AI摘要
本文介绍了两种使用HTTP从网站下载文件的方法。方法一使用IdHttp组件,通过创建文件流并使用IdHTTP1.Get方法下载文件。方法二使用UrlMon组件,通过UrlDownloadToFile函数下载文件,并返回下载结果。两种方法都需要在程序中添加相应的组件和库。
本文介绍了两种使用HTT
用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;