VBS Microsoft.XMLHTTP status

Eugene picture Eugene · Oct 1, 2010 · Viewed 22.4k times · Source

Why does the following code give a 80004005 error when run? I'm trying to get the status of several sites every 10 seconds...(the ones given are examples).

'http://www.sebsworld.net/information/?page=VBScript-URL
'http://www.paulsadowski.com/wsh/xmlhttp.htm

'the array of sites
sites = Array("http://www.google.com/","http://en.wikipedia.org/wiki/Main_Page")

While(True)
    For Each site In sites

        'Get site status
        Set Http = WScript.CreateObject("Microsoft.XMLHTTP")
        Http.Open "GET", site, True
        Http.Send

        If(Http.Status <> 200) Then 'site isn't 200
            MsgBox "The site at " & vbNewLine & site & vbNewLine & "has status: " & Http.Status
        End If
    Next

    WScript.Sleep(10)'Sleep 10 seconds
Wend

Answer

user128300 picture user128300 · Oct 1, 2010

First, you have to change

Http.Open "GET", site, True 

to

Http.Open "GET", site, False

because you cannot use Http.Status immediately after Http.Send if the call is asynchronous.

Furthermore, you shoud use

Set Http = WScript.CreateObject("MSXML2.ServerXMLHTTP") 

instead of

Set Http = WScript.CreateObject("Microsoft.XMLHTTP")

because the normal XMLHTTP object has problems with redirected web sites (www.google.com normally redirects you to another site).