Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6172563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:25:01+00:00 2026-05-23T23:25:01+00:00

An answer to this question showed how easy it is to use WinHTTP via

  • 0

An answer to this question showed how easy it is to use WinHTTP via Type Library imports in delphi.

I imported the type library for WinHTTP, and then tried to write a File Download helper function using that api. Here’s how far I got:

I can’t seem to figure out how to save the IWinHttpRequest.ResponseStream (declared as OleVariant in the TLB file) as Stream, to disk.

// IWinHttpRequest is defined by importing type library of WinHTTP.
// Microsoft WinHTTP Services, version 5.1 (Version 5.1) C:\Windows\system32\winhttp.dll
function Download(const url, filename: String): Boolean;
var
   http: IWinHttpRequest;
   wUrl: WideString;
   fs:TFileStream;
   FileStream:IStream;
   sz,rd,wr:Int64;
begin
  try
   wUrl := url;
   http := CoWinHttpRequest.Create;
   http.open('GET', wurl, False);
   http.send(EmptyParam);

   FStatus := http.status; // 200=OK!
   result := FStatus=200;


   if result then
   begin
     fs := TFileStream.Create(filename, fmCreate, fmShareExclusive );
     try
      FileStream := TStreamAdapter.Create(fs, soReference) as IStream;
      sz := http.ResponseStream.Size;
      http.ResponseStream.CopyTo(FileStream,sz,rd,wr);
     finally
         FileStream :=  nil;
         fs.Free;
     end;
   end;
  except
      result := false;
      // do not raise exceptions.
  end;
end;

Excerpt from WinHTTP_TLB.pas:

 IWinHttpRequest = interface(IDispatch)
    ['{016FE2EC-B2C8-45F8-B23B-39E53A75396B}']
    ......
    property ResponseStream: OleVariant read Get_ResponseStream;

Update: I now get a runtime exception about ole variants, at the call to http.ResponseStream.CopyTo(...)

 EOleError 'Variant does not reference an automation object'.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:25:01+00:00Added an answer on May 23, 2026 at 11:25 pm

    Warren, you must use the AxCtrls.TOleStream class to comunicate the response stream with the Classes.TFileStream

    something like this

    IWinHttpRequest.ResponseStream -> TOleStream -> TFileStream
    

    Check this sample code

    {$APPTYPE CONSOLE}
    
    uses
      Variants,
      ActiveX,
      Classes,
      AxCtrls,
      WinHttp_TLB,
      SysUtils;
    
    
    function Download(const url, filename: String): Boolean;
    var
       http: IWinHttpRequest;
       wUrl: WideString;
       fs:TFileStream;
       HttpStream :IStream;
       sz,rd,wr:Int64;
       FStatus : Integer;
       OleStream: TOleStream;
    begin
      try
       wUrl := url;
       http := CoWinHttpRequest.Create;
       http.open('GET', wurl, False);
       http.send(EmptyParam);
    
       FStatus := http.status; // 200=OK!
       result := FStatus=200;
    
       if result then
       begin
        HttpStream:=IUnknown(http.ResponseStream) as IStream;
        OleStream:= TOleStream.Create(HttpStream);
        try
          fs:= TFileStream.Create(FileName, fmCreate);
          try
            OleStream.Position:= 0;
            fs.CopyFrom(OleStream, OleStream.Size);
          finally
            fs.Free;
          end;
        finally
          OleStream.Free;
        end;
       end;
    
      except
          result := false;
          // do not raise exceptions.
      end;
    end;
    
    
    begin
      try
        Download('http://foo.html','C:\Foo\anyfile.foo');
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to answer this question I found that the code int* p =
Also can you please answer this question? how do I get co-ordinates of selected
(Came up with this question in the course of trying to answer this other
I did some googling to try to answer this question but even after that
Thanks in Advance for reading and answer this question. I got button in asp
I have looked around on the Internet trying to answer this question. It seems
In this answer to this question , Lachlan-Hunt writes the following: With HTML5, you
In answer to this question Joel Coehoorn said Finally, only after the site's gone
The answer to this question: Declarative Security--what's this? Doesn't really tell me much... doesn't
An answer to this question has led me to look into using Event Tracing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.