【问题标题】:Where to put the sleep function in MSXML request?在 MSXML 请求中将睡眠功能放在哪里?
【发布时间】:2021-04-28 03:14:32
【问题描述】:

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

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

【问题讨论】:

标签: vbscript asp-classic msxml2


【解决方案1】:

如果使用 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

【讨论】:

  • 谢谢。只是为了可靠的使用,这个答案只是解决了健康但迟到或长时间响应的问题。要处理错误 500 或 IP 解决问题等其他错误,我必须添加更多错误处理选项。
猜你喜欢
  • 2012-10-04
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 2011-02-03
  • 1970-01-01
  • 2021-04-12
相关资源
最近更新 更多