在 vbscript 中测试 HTTP 响应



我需要测试来自 Web url 的响应时间,看看它是否响应缓慢。我从一个教程网站上得到了这个(不记得是哪个),但它并没有完全按照我想要的去做:

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

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

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

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-

相关内容

  • 没有找到相关文章

最新更新