Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

vbscript - 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
question from:https://stackoverflow.com/questions/65865738/where-to-put-the-sleep-function-in-msxml-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...