Link to home
Start Free TrialLog in
Avatar of Sunef
Sunef

asked on

Using MSXML.XMLHTTPRequest asyncronously in VB6

Im building an activex-dll in VB6 that needs to do a large number of posts every hour. I have built one version already using mswinsck.ocx, but i would prefer to use a control or other webinterface that is already included in the windows/ie package.

I would like to use the MSXML.XMLHTTPRequest asyncronously by setting onreadystatechange to my specified callback function. Unfortunatly in VB6 i know of no way to do that other then the AdressOf keyword, and it doesnt work. Here is a quote from MS at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmproonreadystatechangehttprequest.asp:

"onreadystatechange Property (IXMLHTTPRequest).
Specifies the event handler to be called when the readyState property changes. Note that onreadystatechange is designed for use in scripting environments and is not readily accessible in Microsoft® Visual Basic® or C++."

To me this indicates that its possible to use the asyncronous functionality of MSXML.XMLHTTPRequest in none scripting languages but that it is hard. Someone enlighten me please.

Avatar of mdougan
mdougan
Flag of United States of America image

I don't know about the XMLHTTPRequest, but it is possible to do asyncronous HTTP requests through VB by calling the WinINET API.  If you specify asyncronous, then you provide a callback (through addressof I think), and this does work.  The callback then gets a status code which tells you what is happening.

I have a class that wraps this if you're interested. But it might need a little cleaning up....
Avatar of Anthony Perkins
mdougan,

I am very impressed if you managed with Visual Basic to get the WinInet API to work asynchronously.  Everything I tried and read implied that this was not possible.

Sunef,
>> To me this indicates that its possible to use the asyncronous functionality of MSXML.XMLHTTPRequest
in none scripting languages but that it is hard. Someone enlighten me please.<<

Actually, the way I interpret it, quite the opposite.  In other words the ReadyState and OnReadyStateChange events are NOT readily accessible from Visual Basic or Visual C++.  They are designed only for scripting languages, although I have yet to see a good example of asynchronous XMLHTTPRequest.

Anthony


ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sunef
Sunef

ASKER

modugan: I would love if you could share your wininet code and reward you accordingly. My email is sunef@hotmail.com

bhess1: Thanks thats all the answer i needed. I will accept answer as soon as i get response from modugan.

Is there a way to increase and share the points between 2 users?
Avatar of Sunef

ASKER

I descided to set the callback class to Me and declare the callback function to the default function.

Any objections to this? Code currently looks like:


Option Explicit
Private myMSXML As MSXML.XMLHTTPRequest

Private lastAction As String

Event Response(ByVal Response As String, lastAction As String)

Private Sub Class_Initialize()
  Set myMSXML = New MSXML.XMLHTTPRequest
End Sub


Private Sub Class_Terminate()
  Set myMSXML = Nothing
End Sub


Public Sub Post(ByVal URL As String, ByVal PostString As String, Action As String)

  lastAction = Action

  myMSXML.abort
  myMSXML.open "POST", URL, True
  myMSXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  myMSXML.setRequestHeader "User-Agent", "Meginjarder 2.00"
  myMSXML.OnReadyStateChange = Me
  myMSXML.send PostString
 
End Sub

Function OnReadyStateChange()

  Dim strHttpResponse As String
 
  strHttpResponse = myMSXML.responseText
  RaiseEvent Response(strHttpResponse, lastAction)

End Function
Avatar of Sunef

ASKER

And i have one additional question aswell. What is the proper way to handle errors sich as timeout? There is no state that says "Error" so i guess i have to use a timer and check state after a certain amount of time.
Avatar of Sunef

ASKER

And i have one additional question aswell. What is the proper way to handle errors sich as timeout? There is no state that says "Error" so i guess i have to use a timer and check state after a certain amount of time.
Here is an article that you may find useful:

Time-Outs in XMLHTTP, and Controls with a View
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dn_voices_askgui/html/askgui05012001.asp

Anthony
I'm not sure about 'Me', since I haven't tested it (and currently don't have time to).  If it doesn't work, the other method still will, so I'm not too worried about it.

As for Timeout issues - the timer approach seems to work best.  XMLHTTP doesn't support a timeout concept, so your program will have to control it internally.
Avatar of Sunef

ASKER

And i have one additional question aswell. What is the proper way to handle errors sich as timeout? There is no state that says "Error" so i guess i have to use a timer and check state after a certain amount of time.
Hi.  I sent off a sample project that will let you test the HTTP and FTP classes I have.  I got the original code from Microsoft, and re-wrote a lot of it for my own needs.

I remember that I did suffer through a lot of GPFs until I got the Asynch/callback stuff to work.  I've had the Asynch working on the FTP with the call back for a long time with good results.  

When I looked back through the code I'd had the call back stuff for HTTP commented out (look in the OpenInternet method of the HTTP class, I've uncommented it and I don't get any errors...).  I tried a test using the callback, but I never stopped on a breakpoint there (I think it's dangerous to set a breakpoint in the callback....but I gave it a shot.)  

So, you can play with it and see if it gives you any useful info.  

If you get timeout errors, I think that you can check your return codes and then do a GetLastDllError or something like that.  I've got all of the error handling code in the class, so, give it a look.

Cheers!
Mike

Oh, and when using the sample, you click the send request button to do the get or post to the web address you provide, and all this does is return the headers.  Then, you have to click on one of the tabs (third tab I think) and click the Get Data button to actually return the HTML, normally you'd wrap all of those calls together...
Avatar of Sunef

ASKER

This is just what i was looking for. I ended up using my WebRequest class as callback aswell as all other functionality by defining XmlHttpRequest.onreadystatechange = Me.

During this process i also played with polling getState with a timer and it also achieved what i wanted. But defining a callback is ofcourse much cleaner.