2012-05-28 86 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-