views:

345

answers:

3

Here's the gist:

I have a call I want to make in asp, and I do not care about the response. I just want to fire the call and I do not want the page to wait for the response. According to the documentation, it should look something like this:

dim xmlhttp : set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST", url, true '' setting the 'asynchronous' option to 'true'
xmlhttp.setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"
xmlhttp.setRequestHeader "Content-Length", Len(XMLData)
xmlhttp.send XMLData

This works peachy when calling synchronously, but when I flip the ansynchronous option to 'true', nothing fires. What I can gather from the internet is that users do something like the following:

While xmlhttp.readyState <> 4
    xmlhttp.waitForResponse 1000
Wend

Am I crazy in that this does not really seem like an asynchrous call anymore though if you are waiting for a response?

putting the line xmlhttp.waitForResponse 1 right after the send will cause the request to fire, but again, I don't want to wait a second.

Any thoughts?

+2  A: 

The key problem here is if you don't wait and your script ends the ServerXMLHTTP component destroys itself and in the process aborts the outstanding request. There is no way for you to guarantee where the request has got to at that time.

For example if your server hasn't got round to issuing to the destination server it will see that its no longer needed and not bother.

Even if a connection has been made to the destination server the request may not yet have been given to a handler. Often a web server will check that client is still connected before committing resources to fulfilling a request. If it sees your connection has been dropped it won't bother completing the request.

In other words there is no reliable way to perform this operation asychronously in classic ASP, it just isn't designed to handle that sort of thing. The best you can get is to do other stuff while your script gets on with something else (if you've got anything else to be getting on with), however, I wouldn't even recommend that since asynchronous WinHTTP inside ASP is flaky.

AnthonyWJones
+1  A: 

We use async XMLRequest to log errors into Fogbugz in our ASP sites. As its only an error report we don't want our users hanging around waiting for our code to finish so we do it async. This could be for anything from a missing config file, DB timeout, missing lookup in a config file somewhere, etc. Not always mission crytical stuff but good to know about. In those cases the async works a treat and if not then its not the end of the world for us but we've not had any problems with it. We've been using this script which we create and posted in another question:

http://stackoverflow.com/questions/933553/system-net-httpwebrequest-in-classic-asp/934801#934801

Like Anthony says though its not 100% guanenteed to get through. As a possible fix you could sert Response.Buffer = true, render out all your output to the user, call Response.Flush and then do a waitForResponse call. The user will see the whole page and be able to interact with it without any hold up and it gives your async call a bit more time to finish.

Pete Duncanson
+1  A: 

Hello guys! Today I got the same issue. I resolved it by using "Microsoft.XMLHTTP" object.

dim xmlhttp 
set xmlhttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "POST", url, true 
xmlhttp.Send ""

And now the request is sent asynchronously and target URL was hit. Hope that helps.

Anton Palyok
excellent - thanks!
EdenMachine