Vbscript错误处理

时间:2014-03-14 13:52:41

标签: vba vbscript error-handling

我有一个vbscript,它向网站发送post命令并收集数据。 我遇到的问题是,有时网页变得不可用,并且vbscript以错误结束:800C0005"系统找不到指定的资源"

通过一些搜索我一直在尝试使用下面的代码,但还没有工作

Dim xmlhttp
Set xmlhttp = createobject("msxml2.xmlhttp.6.0")

xmlhttp.open "post", "https://website.com", False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send
Get_HTML = xmlhttp.responsetext

On Error Resume Next

If Err.Number <> 0 Then
  Err.Clear
End If

2 个答案:

答案 0 :(得分:3)

如果要在发生错误时重试操作,则需要在操作之前放置OERN,在出现错误时添加一些延迟,并将整个内容包装在一个循环中:

Set xmlhttp = CreateObject("Msxml2.XMLHttp.6.0")

On Error Resume Next
Do
  Err.Clear  'clear whatever error you had in the last iteration

  xmlhttp.open "POST", "https://website.com", False
  xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  xmlhttp.send
  Get_HTML = xmlhttp.responseText

  If Err Then WScript.Sleep 2000  'wait 2 seconds after an error occurred
Loop While Err
On Error Goto 0

答案 1 :(得分:0)

您希望在执行On Error Resume Next请求之前更改要执行的xmlhttp.open的位置。

Dim xmlhttp
Set xmlhttp = createobject("msxml2.xmlhttp.6.0")

On Error Resume Next

xmlhttp.open "post", "https://website.com", False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send
Get_HTML = xmlhttp.responsetext

If Err.Number <> 0 Then
  'Code for how you want to handle the error, if you want to handle it at all
End If