在vbscript中测试HTTP响应

时间:2012-05-28 18:27:19

标签: http vbscript timeout

我需要测试来自网址的响应时间,看看它是否响应缓慢。我有一个来自教程网站(不记得哪个),但它没有完全符合我的要求:

On Error Resume Next 
Set XMLHttp = CreateObject("MSXML2.ServerXMLHTTP") 
xmlhttp.open "GET", "http://website.url.com" , 0 
xmlhttp.send "" 
If Err.Number < 0 OR Err.Number > 0 Then 
  Dim objShell
  Set objShell = WScript.CreateObject ("WScript.shell")
  MsgBox "TimeOut"
  Set objShell = Nothing
  WScript.Quit 
Else
  MsgBox "OK"
End If 

Set xmlhttp = Nothing

此脚本仅测试网站是否超时。我需要更详细的信息,例如,即使它没有超时,响应时间有多长等等。

1 个答案:

答案 0 :(得分:4)

好的,我找到了解决方案:

Dim strHost

' Put your server here
strHost = "localhost"

if Ping(strHost) = True then
    Wscript.Echo "Host " & strHost & " contacted"
Else
    Wscript.Echo "Host " & strHost & " could not be contacted"
end if

Function Ping(strHost)

    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
        Ping = False
            'WScript.Echo "Status code is " & objRetStatus.StatusCode
        else
            Ping = True
            'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
            Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
            'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
        end if
    next
End Function 

所有积分都会转到此网站,而且是作者:http://www.windowsitpro.com/article/vbscript/how-can-i-use-a-vbscript-script-to-ping-a-machine-