Search code examples
vbscriptasp-classicmsxml2

Where to put the sleep function in MSXML request?


I use the following function to check if a RSS url is healthy then consume it:

function testUrl(url)
    testUrl=0
    Set o = CreateObject("MSXML2.XMLHTTP")
    on error resume next
    o.open "GET", url, false
    o.send
    if o.Status = 200 then testUrl = 1
    on error goto 0
    set o=nothing
end function

However when the target URL does not respond in a short time I will get timeout error. So I want to use the following function in this Q/A to terminate the request after 5 seconds if there was no success response but I dont know where to put the asp_Wait(5) and how to cancel the request after 5 seconds? Should I put asp_Wait right after the o.send or o.send acts synchronous?

Function asp_Wait(nMilliseconds)
  Dim oShell
  Set oShell= Server.CreateObject("WScript.Shell")
  Call oShell.run("ping 1.1.1.1 -n 1 -w " & nMilliseconds,1,TRUE) 
End Function

Solution

  • If using the WinHTTPRequest object you can call the WaitForResponse method.

    Set o = CreateObject("MSXML2.XMLHTTP")
    o.open "GET", url, true 'async request
    o.send
    If o.waitForResponse(5) Then 'wait 5 sec
       ...
    Else 'wait timeout exceeded
       ...
    End If