Zulake
Zulake

Reputation: 149

Vbscript error handing

I have a vbscript that sends a post command to a website and collects data. The problem I have is that sometimes the webpage becomes unavailable and the vbscript ends with an error: 800C0005 "The system cannot locate the resource specified"

With some search I have been trying to use the code below, but hasn't been working

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

Upvotes: 1

Views: 1996

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

If you want to retry an operation in case of an error you need to put the OERN before the operation, add some delay when you got an error, and wrap the whole thing in a loop:

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

Upvotes: 3

JustSomeQuickGuy
JustSomeQuickGuy

Reputation: 943

You want to change the position of On Error Resume Next to execute before executing the xmlhttp.open request.

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

Upvotes: 1

Related Questions