用HTTP从网站下载文件

samool 发布暂无评论8293 阅读

AI摘要

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

用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;

  1. 01SaMDeditor V2.4 更新内容2026-12-02
  2. 02恭喜《轻武爱学习》小程序备案成功!2026-11-24
  3. 03SaView 图阅 1.1.1 更新日志2026-11-18
  4. 04Book2EPUB 1.0.4 更新日志2026-11-18
  5. 05SaView 图阅 一款轻便纯粹的看图工具2026-11-16
  6. 06免费工具Book2EPUB:轻松将电子书转换为 EPUB2026-11-11
  7. 07SaBlog2026 主题功能介绍(v1.1.36)2026-10-26
  8. 08PHP2Shell 重大安全漏洞事件深度分析报告2026-10-23
  9. 09LiteLLM v1.106.0 更新内容总结2026-10-20
  10. 10AI Native 受控软件工程2026-10-12
COMMENTS / 一纸留言

读者留言 0 条

一纸留言,便是山水间的一次相逢。

还没有留言,欢迎写下第一段交流。

WRITE A COMMENT

写下你的留言