The VBScript Network and Systems Administrator's Cafe

Oct 3 2008   3:00PM GMT

How to retrieve HTML web pages with VBScript via the Microsoft.XmlHttp object



Posted by: Jerry Lees
HTTP, HTML, XML, VBScript, VBScript Objects, Microsoft.XmlHttp, Web Pages

Recently, I had a situation where I had to pull down a HTML page to compare it to a known copy of the page. Certainly, IE or FireFox– or Google’s Chrome would have done the trick and I could have viewed the source. But that would require me to do work every time we needed to check the page against the known good source.

 Instead I wrote a script to pull the HTML source and echo the response to the console (or a messag box if you are not using cscript to execute the script). While not a full blown HTTP QA script it does do the job of getting the HTTP responses from the server and is certainly a core part of any QA script anyone would write.

Basically the script uses the Microsoft.XMLHTTP object to preform all the HTTP calls and retrieve the HTML page. It sounds scary, but if you look at the script below I think you’ll find that it really is quite easy to accomplish.  So, here is the script’s code:

URL=”http://www.gamersigs.net/
Set WshShell = WScript.CreateObject(”WScript.Shell”)
Set http = CreateObject(”Microsoft.XmlHttp”)

On Error Resume Next
http.open “GET”, URL, False
http.send “”
if err.Number = 0 Then
     WScript.Echo http.responseText
Else
     Wscript.Echo “error ” & Err.Number & “: ” & Err.Description
End If
set WshShell = Nothing
Set http = Nothing

Enjoy!

Comment on this Post


You must be logged-in to post a comment. Log-in/Register