vbscriptmsxmlserverxmlhttp

Handling an MSXML timeout


One of our websites has a glitch which occasionally takes it down - the back end needs to be restarted. The replacement is not yet ready for production, so I've created a VBS script which polls the server and restarts the server if:

a) The HTTP status code <> 200, or b) The page loads but a substring is not found on the page.

The HTTP status check times out if the tomcat service fails to respond; I would like to call a function if that occurs. Is it possible to capture and handle a timeout? This is a snippet of my relevant code pieces.

xmlhttp.setOption 2, 13056
xmlhttp.open "get", "https://www.mywebsite.com/thisurl", false
xmlhttp.send

if xmlhttp.status <> 200 then
    call restartTomcat()
    call emailAlert()
end if

Thank you in advance.


Solution

  • Unfortunately you can't create object event handlers in VBScript.

    Perhaps you can use the .setTimeouts resolveTimeout, connectTimeout, sendTimeout, receiveTimeout method of the HTTP Request object. Add this before your call to the send method. Here's the description of the parameters:

    resolveTimeout

    A long integer. The value is applied to mapping host names (such as "www.microsoft.com") to IP addresses; the default value is infinite, meaning no timeout.

    connectTimeout

    A long integer. The value is applied to establishing a communication socket with the target server, with a default timeout value of 60 seconds.

    sendTimeout

    A long integer. The value applies to sending an individual packet of request data (if any) on the communication socket to the target server. A large request sent to a server will normally be broken up into multiple packets; the send timeout applies to sending each packet individually. The default value is 30 seconds.

    receiveTimeout

    A long integer. The value applies to receiving a packet of response data from the target server. Large responses will be broken up into multiple packets; the receive timeout applies to fetching each packet of data off the socket. The default value is 30 seconds.

    You can check the Status as you are currently doing after the send call. This should help you handle an unresponsive server.