How to retrieve a file from Internet via HTTP?

后端 未结 6 2149
天命终不由人
天命终不由人 2020-12-30 17:46

I want to download a file from Internet and InternetReadFile seem a good and easy solution at the first glance. Actually, too good to be true. Indeed, digging a bit I have s

6条回答
  •  心在旅途
    2020-12-30 18:18

    My personal favorite is using the WebHttpRequest component from importing the "Microsoft WinHTTP Services" type library: http://yoy.be/item.asp?i142

    var
      w:IWebHttpRequest;
      f:TFileStream;  
      os:TOleStream;
    begin 
      w:=CoWebHttpRequest.Create;
      w.Open('GET',SourceURL,false);
      w.Send(EmptyParam);
      os:=TOleStream.Create(IUnknown(w.ResponseStream) as IStream);
      f:=TFileStream.Create(DestinationFilePath,fmCreate);
      os.Position:=0;
      f.CopyFrom(os,os.Size);
      f.Free;
      os.Free;
      w:=nil;
    end;
    

提交回复
热议问题