VBS Microsoft.XMLHTTP状态

时间:2010-10-01 02:21:02

标签: vbscript status xmlhttprequest

为什么以下代码在运行时会出现80004005错误?我试图每隔10秒获得几个站点的状态......(给出的是示例)。

'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 个答案:

答案 0 :(得分:11)

首先,你必须改变

Http.Open "GET", site, True 

Http.Open "GET", site, False

因为如果调用是异步的,则Http.Status之后不能立即使用Http.Send

此外,你应该使用

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

而不是

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

因为普通的XMLHTTP对象在重定向的网站上存在问题(www.google.com通常会将您重定向到另一个网站)。

答案 1 :(得分:1)

Const ForWriting = 2

strURL="http://asithayomal.1apps.com"
Set objHTTP = CreateObject("MSXML2.XMLHTTP") 
Call objHTTP.Open("GET", strURL, FALSE) 
objHTTP.Send

msgbox objHTTP.ResponseText