将睡眠功能放在 MSXML 请求中的何处?

问题描述

我使用以下函数来检查 RSS 网址是否健康,然后使用它:

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

但是当目标 URL 在短时间内没有响应时,我会收到超时错误。因此,如果没有成功响应,我想在 this Q/A 中使用以下函数在 5 秒后终止请求,但我不知道将 asp_Wait(5) 放在哪里以及如何在 5 秒后取消请求?我应该在 o.sendo.send 行为同步之后立即放置 asp_Wait 吗?

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

解决方法

如果使用 WinHTTPRequest 对象,您可以调用 WaitForResponse 方法。

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