<job>
<!--
HTTPING2.WSF - "Ping" a web server.
Version 2.0
This WSH script is used to make HTTP requests of web servers and
display the results returned. It is most useful in determining
whether a web server or application is responding from within
Windows batch files by testing ERRORLEVEL for failure Return
Codes (mostly due to timeouts) and HTTP Status Codes (200 is OK,
the standard good result).
The script is a wrapper for the "w3 Sockets" sock.tcp class, a
free ActiveX component that can be obtained from Dimac Development
at:
[URL unfurl="true"]www.dimac.com[/URL]
There is no charge, but registration is required.
The w3 Sockets component must be properly installed and registered
on any machine you want to run this script on.
Most of the effort here is spent on supporting command line
parameters and "help." Inspecting this script will show that the
logic needed to use w3 Sockets for this purpose is relatively
trivial. If you are writing WSH scripts instead of using batch
files you would probably want to use w3 Sockets directly.
-->
<runtime>
<description>
This script makes an HTTP GET request of a web server and displays
the results returned.
Return Codes:
1 : Bad command line options
10 : Open failed
20 : Send failed
30 : Receive failed
40 : Result display failed
>199 : HTTP Response Status Code, 200 = OK
</description>
<unnamed name="host"
helpstring="Web server to access"
many="false"
required="true"/>
<unnamed name="uri"
helpstring="Resource (page) requested without leading "/" (defaults to root)"
many="false"
required="false"/>
<named name="port"
helpstring="HTTP port (default = 80)"
type="string"
required="false"/>
<named name="timeout"
helpstring="Time to wait for response (in ms, default = 1000)"
type="string"
required="false"/>
<example>
Examples:
cscript //nologo httping.wsf [URL unfurl="true"]www.google.com[/URL] > response.txt
Suppresses WSH logo, GETs default root page of [URL unfurl="true"]www.google.com[/URL] and
redirects the response to the file response.txt for later viewing.
cscript httping.wsf web.foo.com cgi-bin/moeba.cgi?act=status
GETs resource /cgi-bin/moeba.cgi?act=status of web.foo.com and
displays the response text on the console.
cscript httping.wsf web.bar.com /port:8080 /timeout:5000
GETs default root page of [URL unfurl="true"]www.bar.com[/URL] on port 8080, with a timeout
of 5 seconds (5000 ms).
</example>
</runtime>
<object id="W3Sock" progid="socket.tcp"/>
<script language="VBScript">
Option Explicit
Dim Host, URI, Port, TimeOut, Response
Sub GetOptions()
With WScript.Arguments
With .Unnamed
If .Count < 1 Then
WScript.Arguments.ShowUsage
WScript.Quit 1
Else
Host = .Item(0)
If .Count < 2 Then
URI = ""
Else
URI = .Item(1)
End If
End If
End With
With .Named
If .Exists("port") Then
Port = .Item("port")
Else
Port = "80"
End If
If .Exists("timeout") Then
TimeOut = CLng(.Item("timeout"))
Else
TimeOut = 1000
End If
End With
End With
End Sub
Sub Check(ByVal Num, ByVal Desc, ByVal RetCode)
If Num <> 0 Then
WScript.Echo _
"Error " _
& CStr(RetCode) _
& " &H" _
& Hex(Num) _
& " " _
& Desc
W3Sock.Close
WScript.Quit RetCode
End If
End Sub
Sub ReturnHTTPStatus()
Dim Status
Status = InStr(1, Response, " ") + 1
WScript.Quit CLng(Mid(Response, Status, 3))
End Sub
GetOptions
With W3Sock
.Host = Host & ":" & Port
.TimeOut = TimeOut
On Error Resume Next
.Open
Check Err.Number, Err.Description, 10
.SendLine "GET /" & URI & " HTTP/1.0" & vbNewLine
Check Err.Number, Err.Description, 20
.WaitForDisconnect
Check Err.Number, Err.Description, 30
Response = .Buffer
Check Err.Number, Err.Description, 30
WScript.Echo Response
Check Err.Number, Err.Description, 40
.Close
ReturnHTTPStatus
End With
</script>
</job>