Call a .NET Webservice using XMLHTTP,XMLDOM and VBScript

By Peter A. Bromberg, Ph.D.

Peter Bromberg

The appearance of Visual Studio .NET at the Professional Developers Conference in Orlando this July gave birth to a whole new world of exposed http-addressable services and component methods available to developers over the web. The Microsoft code name for this "bundle of joy", as it were, is "webservices". If webservices were a stock, I wouldn't short it just yet. It promises to be one of the most exciting new concepts on the web since Al Gore invented it. If you're not sure why webservices holds such promise, look at the diverse list of authors of the W3C SOAP 1.1 Specification and that should give you a clue:

The Microsoft flavor of webservice is not only incredibly easy to create using the Visual Studio IDE and the language of your choice in the Common Language Runtime (CLR), but they are also callable through SOAP and other methods including a straight HTTP GET with the parameter(s) name and value(s) on the querystring.

Lets take a look at a sample webservice that Microsoft exposes to the public, examine its interface, and write a HTTP page to access this service and present the results.  Actually, unlike most "examples", the page I have in mind is one that is quite useful and that you might very well want to have on your website. It's the MSDN Search Beta "Best Bets" webservice method. What this does is take your search phrase and match it up to a cached set of "Best Bet" matches at MSDN and return the result as well-formed XML.

Here is a link to the  the webservice "disco" page so you can see how a Microsoft webservice exposes itself:

The webservice method we will be using is the second one, "GetBestBets."  To see what the result of a search will look like, click the link below for a search on "XML":

What we'll do now is write a page with the least possible amount of code that will present a search form and display the items from the result XML document in a nice table, converting the "Title" element from each node into a nice hyperlink to its corresponding "Url" element, in a nice HTML table just below the Search form. We'll do this in a narrow 130 pixel wide table column so that the code can be inserted on the left or right side column of your web page.

First let's take a look at all the code, and then we'll walk through it:

<script language="VBScript" runat=Server>
function getResults()
Dim xmlDOC
Dim bOK
Set HTTP = CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =CreateObject("MSXML.DOMDocument")
HTTP.Open "GET","" 
& document.all("SearchTerm").value , False
bOK = xmlDOC.load(HTTP.responseXML)
if Not bOK then
	rtext.innerText = "Error loading XML from HTTP"
end if
' Note: Instead of making an XSL transform Stylesheet, we will use the selectNodes Method 
' with the XPath "//" search directive to get the ifornmation we want to display out of the
' XML DOMDocument. We'll use this to create our HTML Table "on the fly"
Dim objNodeListUrl
DIm objNodeListTitle
Set objNodeListUrl=xmlDOC.documentElement.selectNodes("//Url")
Set objNodeListTitle=xmlDOC.documentElement.selectNodes("//Title")
rtext.innerText = "Len: "& objNodeListUrl.Length
Dim I
Dim sTable
sTable = "<tableborder:1px solid black"" width=""130"">"
sTable=sTable &  "<trfont-size:8pt; font-family:Verdana; font-weight:bold; "
sTable =sTable & " text-decoration:underline"">"
sTable = sTable & "<td align=""center"">Results:</TD>"
For I = 0 to objNodeListUrl.Length -1
	sTable = sTable & "<trfont-family:Verdana; font-size:8pt; padding:0px 2px""><td>"
	sTable = sTable & "<a href=" & objNodeListUrl(i).text & ">"
	sTable = sTable & objNodeListTitle(i).text & "</a></TD>"
sTable=sTable & ""
rtext.innerHTML = sTable
stat.innerText ="Status: " & HTTP.statusText
end Function
<div align=Center><font face=Tahoma size=2><b>MSDN "Best Bets":</b><br>
  Enter Search Term</FONT><b><br>
  <input type="text" name="SearchTerm" value="" size=12>
  <input type="button" value="Get Results" onClick="getResults()">
<div id=rtext></div><p>
<div id=stat></div>


The first part of our code is the VBScript function getResults(). This is called via the onClick event of the button control, and sets references to the XMLHTTP object we'll use to send our request, and the XMLDOMDocument object we'll use to hold the well-formed XML document that the webservice returns to our page.

We set the async property to "false" - we want to wait until our result comes back. We call the XMLHTTP.Open method with "GET" because that is what the webservice is looking for. To the querystring, we append the value for SearchTerm that the user entered into the form with document.all("SearchTerm").value, and then we call the Send() method to send our XMLHTTP request over the wire.

If everything works, we get back our response XML in the xmlDOC.load(HTTP.responseXML) and our XMLDOMDocument is in xmlDOC.

Now we can do whatever we need to get out the pertinent information and display it the way we want. Normally I would load an XSL Styesheet in a separate DOMDocument and call xmlDOC.transformNode(xslDOC) to get my HTML for display. But here, we really need such a small amount of information from the xml aggregate that instead we'll use some XPATH to "extract" the values from the nodelists we need, and simply create our HTML table "inline".

So the next step is that we define two nodelists with:

Dim objNodeListUrl
Dim objNodeListTitle
Set objNodeListUrl=xmlDOC.documentElement.selectNodes("//Url")
Set objNodeListTitle=xmlDOC.documentElement.selectNodes("//Title")

This give us two node lists, one containing all the "Url" element values, and the other containing all the "Title" values, and now we have everything we need to construct our table of titles and associated hyperlinks.

We start building our table HTML in the strTable variable, and iterate through the nodelists with:

For I = 0 to objNodeListUrl.Length -1

and we concatenate our string with the objNodeListUrl(i).text and objNodeListTitle(i).text values. The result is a string containing our HTML table, and we set the value of the <div> tag rtext.innerHTML to this string. To try it out, Click here. You can get the source by simple saving the page after you've tried it.