2012-05-28 50 views
1

我需要测试web网址的响应时间,看它是否得到缓慢的响应。我有这个从教程网站(不记得是哪),但它不这样做正是我想要的:在vbscript中测试HTTP响应

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 

这个脚本只有当网站timesout或不测试。我需要更详细的信息,例如,即使它不超时,有多长的响应时间等

回答

4

OK,我找到了解决办法:

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-