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 6965973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:07:52+00:00 2026-05-27T16:07:52+00:00

The code: var WinHttpReq: OleVariant; procedure TForm1.Button1Click(Sender: TObject); begin WinHttpReq := CreateOleObject(‘WinHttp.WinHttpRequest.5.1’); WinHttpReq.Open(‘GET’, ‘http://stackoverflow.com’,

  • 0

The code:

var
  WinHttpReq: OleVariant;

procedure TForm1.Button1Click(Sender: TObject);    
begin
  WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
  WinHttpReq.Open('GET', 'http://stackoverflow.com', TRUE); // asynchronously
  WinHttpReq.setRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0');
  WinHttpReq.Send();
  // HOW to set a callback procedure here and get the response?
end;

Note: I do not want to import mshttp.dll and use TLB. I want to use it via late binding. I also would like to handle exceptions if any.

EDIT:
I’m accepting TLama’s answer becouse it gives me a good alternative to what I initially was asking. plus it has a good example source.

Here is a very nice implementation of WinHTTPRequest Wrapper with IConnectionPoint for Events (source code is attached).

  • 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-27T16:07:52+00:00Added an answer on May 27, 2026 at 4:07 pm

    As Stijn said in his answer, to prevent your program to lag, use the threads. IWinHttpRequest.Open has the asynchronous configuration capability too but it would be very difficult to catch the events and IWinHttpRequest.WaitForResponse would stuck your program even so.

    Here is the simple example of how to get the response text into the form’s memo box.
    Please note that the following example uses the synchronous mode and that you can additionally modify the timeout values using IWinHttpRequest.SetTimeouts. If you want to use the asynchronous mode as you have in your question then you’ll have to wait for the result with IWinHttpRequest.WaitForResponse method.

    ///////////////////////////////////////////////////////////////////////////////
    /////   WinHttpRequest threading demo unit   //////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    unit WinHttpRequestUnit;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ActiveX, ComObj, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   THTTPRequest - TThread descendant for single request   ////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    type
      THTTPRequest = class(TThread)
      private
        FRequestURL: string;
        FResponseText: string;
        procedure Execute; override;
        procedure SynchronizeResult;
      public
        constructor Create(const RequestURL: string);
        destructor Destroy; override;
      end;
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   THTTPRequest.Create - thread constructor   ////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    // RequestURL - the requested URL
    
    constructor THTTPRequest.Create(const RequestURL: string);
    begin
      // create and start the thread after create
      inherited Create(False);
      // free the thread after THTTPRequest.Execute returns
      FreeOnTerminate := True;
      // store the passed parameter into the field for future use
      FRequestURL := RequestURL;
    end;
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   THTTPRequest.Destroy - thread destructor   ////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    destructor THTTPRequest.Destroy;
    begin
      inherited;
    end;
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   THTTPRequest.Execute - thread body   //////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    procedure THTTPRequest.Execute;
    var
      Request: OleVariant;
    begin
      // COM library initialization for the current thread
      CoInitialize(nil);
      try
        // create the WinHttpRequest object instance
        Request := CreateOleObject('WinHttp.WinHttpRequest.5.1');
        // open HTTP connection with GET method in synchronous mode
        Request.Open('GET', FRequestURL, False);
        // set the User-Agent header value
        Request.SetRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0');
        // sends the HTTP request to the server, the Send method does not return
        // until WinHTTP completely receives the response (synchronous mode)
        Request.Send;
        // store the response into the field for synchronization
        FResponseText := Request.ResponseText;
        // execute the SynchronizeResult method within the main thread context
        Synchronize(SynchronizeResult);
      finally
        // release the WinHttpRequest object instance
        Request := Unassigned;
        // uninitialize COM library with all resources
        CoUninitialize;
      end;
    end;
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   THTTPRequest.SynchronizeResult - synchronization method   /////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    procedure THTTPRequest.SynchronizeResult;
    begin
      // because of calling this method through Synchronize it is safe to access
      // the VCL controls from the main thread here, so let's fill the memo text
      // with the HTTP response stored before
      Form1.Memo1.Lines.Text := FResponseText;
    end;
    
    ///////////////////////////////////////////////////////////////////////////////
    /////   TForm1.Button1Click - button click event   ////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////
    
    // Sender - object which invoked the event
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      // because the thread will be destroyed immediately after the Execute method
      // finishes (it's because FreeOnTerminate is set to True) and because we are
      // not reading any values from the thread (it fills the memo box with the
      // response for us in SynchronizeResult method) we don't need to store its
      // object instance anywhere as well as we don't need to care about freeing it
      THTTPRequest.Create('http://stackoverflow.com');
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
Code: var result = db.rows.Take(30).ToList().Select(a => AMethod(a)); db.rows.Take(30) is Linq-To-SQL I am using ToList()
See code: var file1 = 50.xsl; var file2 = 30.doc; getFileExtension(file1); //returns xsl getFileExtension(file2);
for example this code var html = <p>This text is <a href=#> good</a></p>; var
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
in the following code : var benq:Base64Encoder = new Base64Encoder(); benq.encode(force,0,5); var tmp:String =
Given the following code: var people = new List<person>(){ new person { Name =
I have this code: var $msg = jQuery('<div></div>') .hide() .appendTo(document.body) ; if ($msg.is(:hidden)) {
I have the following code: var tempIdx = document.getElementById('cityBuild').selectedIndex; var tempBuilding = document.getElementById('cityBuild').options[tempIdx].value; //
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });

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.