views:

32

answers:

1

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
+2  A: 

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).

fmunkert
That makes sense. Thanks!
(should also be 10000 for 10 seconds)