{
关键字=idhttp下载文件支持https
采集软件=MKM - 我的知识管理
采集日期=2023-11-23 13:26:53
数字签名=9595D66A23FE0625F966E8A4546B75D8
}
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms,
Dialogs, IdIOHandler, IdIOHandlerSocket, IdSSLOpenSSL,
IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls,
IdIOHandlerStack,
IdSSL;
function TForm1.DownloadFile(aurl: string; destfile: string): string;
var
MemoryStream: TMemoryStream;
URL, SavePath, sFileName, sFileType: string;
DMHttp: TIdHTTP;
begin
{idhttp下载文件支持https}
Result := ‘’;
if aurl = ‘’ then
Exit;
URL := ‘’;
SavePath := ‘’;
sFileName := ‘’;
sFileType := ‘’;
try
MemoryStream := TMemoryStream.Create;
DMHttp := TIdHTTP.Create(nil);
URL := aurl;
Screen.Cursor := crHourGlass;
if aurl.StartsWith(‘https’) then
begin
DMHttp.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
DMHttp.HandleRedirects := True;
end;
try
DMHttp.Get(URL, MemoryStream);
MemoryStream.Position := 0;
sFileName := DMHttp.Response.RawHeaders.Values['Content-Disposition'];
Delete(sFileName, 1, Pos('filename=', sFileName) +
Length('filename=') - 1);
if destfile.Contains('\\') then
begin
SavePath := destfile;
end
else
begin
SavePath := ExtractFilePath(Application.ExeName) + '\\' + destfile;
end;
MemoryStream.SaveToFile(SavePath);
Result := SavePath;
except
end;
finally
FreeAndNil(MemoryStream);
FreeAndNil(DMHttp);
Screen.Cursor := crDefault;
end;
end;