2

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
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82

1 Answers1

1

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
user2316116
  • 6,726
  • 1
  • 21
  • 35
  • Thank you. Just for a reliable use, this answer just solves the issue for healthy but late or long time responses. To handle other errors like error 500 or IP resolve problems, I had to add more error handling options. – Ali Sheikhpour Jan 26 '21 at 18:12