[yoy - Why-O-Why]yoy - “Why-O-Why”

» frame | home browse filter search | refresh | log on

  printed vrijdag 31 oktober 2014 8:31:17 from www.yoy.be

how-to
Comment...
Add another
Add child
Edit
Move
Delete
Add token
Add reference

How to get a file from a URL. (WinHttp IWebHttpRequest.ResponseStream)

In all recent MicrosoftWindows versions, a COM component is available that allow to perform HTTP transfers. In the system folder, winhttp.dll defines the WinHttp.WinHttpRequest object that exposes the IWebHttpRequest interface.

In Delphi, generate a wrapper unit for the TypeLibrary by menu option "Project > Import Type Library" and then select "Microsoft WinHTTP Services" from the list.

Use the Open method to set the URL and method ("GET" or "POST"). The Send method sends the request. When done, the response is available in one of the Response properties.

ResponseText provides the contents of the response as a WideString.
ResponseBody provides the contents of the response as an array of unsigned bytes.
ResponseStream provides the contents of the response through an IStream.

To write the response to a file, include AxCtrls to the uses clause, and write code similar to this:

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;

location: Application Development > Programming Languages > Object Pascal > Delphi > ActiveX / COM objects
created: 14/07/2005 11:12:55 « modified: 14/07/2005 11:12:55 (diff) weight: 0

children, tokens, references comments (4) references to object (1)

see also -- HTTP edit   del   created: 1/03/2010 11:18:55 « modified: 1/03/2010 11:18:55 weight: 0
see also -- COM edit   del   created: 1/03/2010 11:19:41 « modified: 1/03/2010 11:19:41 weight: 0
see also -- URL edit   del   created: 1/03/2010 11:20:03 « modified: 1/03/2010 11:20:03 weight: 0
see also -- Microsoft Windows edit   del   created: 1/03/2010 11:20:24 « modified: 1/03/2010 11:20:24 weight: 0
MicrosoftWindows