PaulSadowski.com 

Windows Scripting Host
 VBScript Using XmlHttp
The XMLHTTP object is a hand tool for getting information from web servers. Below I'll show several examples of how this object can be used.
  • GetHTMLSource - this script displays the HTML source for a given web page.
  • HTMLHead.vbs - this script gets makes a HEAD request to a web server and displays all responses from it.
  • ScrapeLinks.vbs - displays a list of links and their associated link text on a given web page.

• ScrapeLinks uses Internet Explorer rather than the XMLHTTP object but it fits nicely here with the other scripts.

These scripts should be run from the command prompt using CScript.


'GetHTMLSource.vbs

args = WScript.Arguments.Count

if args <> 1 then
  Wscript.Echo "usage: GetHTMLSource.vbs URL"
  wscript.Quit
end if

URL = WScript.Arguments.Item(0)

Set WshShell = WScript.CreateObject("WScript.Shell")

Set http = CreateObject("Microsoft.XmlHttp")
http.open "GET", URL, FALSE
http.send ""
WScript.Echo http.responseText

set WshShell = nothing
set http = nothing

 


'HTMLHead.vbs

args = WScript.Arguments.Count

if args <> 1 then
  Wscript.Echo "usage: HTMLHead.vbs URL"
  wscript.Quit
end if

URL = WScript.Arguments.Item(0)

Set WshShell = WScript.CreateObject("WScript.Shell")

Set http = CreateObject("Microsoft.XmlHttp")
http.open "HEAD", URL, FALSE
http.send ""
WScript.Echo http.getAllResponseHeaders

' You can display a specific header by using the getResponseHeader method:
' Wscript.echo http.getResponseHeader("Content-Length")
' You can use the Status and/or StatusText properties if all you want is the
' page status information:
' Wscript.echo http.Status & vbTab & http.StatusText
' On XP and above you could use these properties and
' the Win32_Ping object to ping a server to see if it's alive
' and check the status of a web page to verify the web server
' is sending the requested page.


set http = nothing
set WshShell = nothing

 


'ScrapeLinks.vbs

args = WScript.Arguments.Count

if args <> 1 then
  Wscript.Echo "usage: ScrapeLinks.vbs URL"
  wscript.Quit
end if

URL = WScript.Arguments.Item(0)

With CreateObject("InternetExplorer.Application")
  .Navigate URL
  Do Until .ReadyState = 4
    Wscript.sleep 10
  Loop
  for each link in .document.links
    Wscript.echo link, link.InnerText
  next

' Uncomment the three lines below to scrape references to images

'  for each pix in .document.images
'  Wscript.echo pix.src
'  next
 
  .Quit
End With

 

 

© 2003 by Paul R. Sadowski   
All Rights Reserved. Used By Permission.  
Comments to: Scripting