How do I fire an asynchronous call in asp classic and ignore the response?

浪子不回头ぞ 提交于 2019-11-27 06:14:15

问题


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?


回答1:


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.




回答2:


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.




回答3:


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:

System.Net.HttpWebRequest in classic asp?

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.




回答4:


Another option if you still want to use the ServerXMLHTTP object (and not the client-side component mentioned above by Anton see FAQ #4 at http://support.microsoft.com/kb/290761) is to create a VBScript class. Instantiate the class. Set a property of the class to the HTTP object. Then call a method to fire off the request.

Don't ever destroy the class instance - it will be automatically destroyed when the ASP page finishes. Have the class destruction method free the HTTP object - that should give it enough time to fire off the request before it is destroyed.



来源:https://stackoverflow.com/questions/1510497/how-do-i-fire-an-asynchronous-call-in-asp-classic-and-ignore-the-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!