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).
As Stijn said in his answer, to prevent your program to lag, use the threads.
IWinHttpRequest.Openhas the asynchronous configuration capability too but it would be very difficult to catch the events andIWinHttpRequest.WaitForResponsewould 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 withIWinHttpRequest.WaitForResponsemethod.