Downloads
22043.zip

When you use a browser to navigate to a URL, your browser receives and displays an HTML page, which frequently contains much more information than you need. For example, suppose that you want to know the weather forecast for a certain city. You might point your browser to http://www.weather.com, enter a city or ZIP code, and wait for a new page to be posted. When the page appears, it contains a lot of graphics and advertising in addition to the eagerly awaited information.

With the proper string in the address bar, you can speed up the forecast-retrieval process and display just the forecast without the embellishments. Typically, the Web server obtains the target information by using a URL with trailing parameters that the server needs to accomplish its search. If you know how to build the parameterized URL, you can get the information you want more quickly. Another good example of using direct and parameterized URLs is the Nasdaq Web page that lets you specify quote symbols in the URL composition.

This talk of parameterized URLs raises a couple of questions for VBScript programmers: Is there an easy way to call a URL (parameterized or not) from within VBScript? Can you perform HTTP programming from VBScript? As you already might have guessed, the answer to both questions is yes.

XmlHttpRequest's Main Methods
The XmlHttpRequest component comes with Microsoft Internet Explorer (IE) 5.0 and later and with Microsoft XML Parser (MSXML) 3.0 and later. If you're running Windows 2000, you already have this component installed and properly registered. If you're running Windows NT, make sure you have IE 5.0 or MSXML 3.0, or later versions of these products. Let's see how to use XmlHttpRequest from VBScript.

The XmlHttpRequest object has the programmatic identifier (ProgID) Microsoft.XMLHTTP. You create a new instance of this component with the code

Set xmlhttp = CreateObject _
  ("Microsoft.XMLHTTP")

Two methods—Open and Send—play a fundamental role in XmlHttpRequest's programming interface. You first use the component's Open method to set the HTTP command and the target URL, as in

xmlhttp.open "GET", _
  "http://server/page.asp", false

The URL must be an absolute URL and not a relative Web path. The third argument is a Boolean parameter that indicates whether you want the operation to occur synchronously or asynchronously. The default value, True, means an asynchronous operation. The code example synchronously connects to the URL and uses the GET command to download the HTML code at that address.

The Open method can also take username and password arguments that the Web server can use to authenticate users before they can access the URL. The user and password parameters can be empty or missing unless the site requires authentication. In this case, the component displays a logon window to connect.

The Open method doesn't start an operation—it just prepares the object, which waits until the Send method explicitly tells the object to get moving:

xmlhttp.send ""

If the call to Open specifies an asynchronous operation, you can check the value of XmlHttpRequest's ReadyState property to find out whether the call has been completed. When the value of this property is set to 4, the document has been completely loaded, either successfully or not.

Connecting to a Web Page
To test the XmlHttpRequest component, let's write a simple Active Server Pages (ASP) page and a simple script to return it. Listing 1 shows the code for a simple test page called Test.asp. Listing 2 shows a script that returns Test.asp.

When you connect to a Web page, you see all the code that the Web server returns for that URL. Typically, the page consists of HTML text, but it could also include other types of content, such as XML and JPEG. Web developers set the content types in the body of the server page. In ASP files, the Response.ContentType property defines content types. The text/html string identifies HTML text.

After your browser receives the complete response from the Web server, XmlHttpRequest packages the response and makes it available in a variety of formats. The code at callout A in Listing 2 uses the ResponseText property to process the returned output as plain text. In this case, you see an in-memory copy of the page as IE's View, Source menu shows it.

Another possible format for the output is XML. For this format, you use the ResponseXml property, which is available only when the content type of the server page is set to text/xml and the output is a well-formed XML document. In the case of an invalid content type or format, the ResponseXml property simply returns the keyword Nothing. Two other properties—Response-Stream and ResponseBody—format content as a stream or an array of bytes, respectively.

The code in Listing 3, which is only slightly more complex than the code in Listing 2, downloads the content of a Web page. The code then uses the FileSystemObject object to save the page locally.

XmlHttpRequest's Other Methods
One way to think about the XmlHttpRequest component is as a sort of object model built on top of HTTP because calls to the component always originate HTTP operations. The COM wrapper around the HTTP rules makes this interesting and powerful functionality available to VBScript applications.

You've already seen how XmlHttpRequest's two key methods, Open and Send, configure the environment, set the type of request (e.g., GET, POST), and issue a call. XmlHttpRequest can also work with HTTP headers. For example, you can obtain information from any header that's in the HTTP response to your request. Use the code

obj.GetResponseHeader _
  ("Content-Type")

to access on the client the content type of the freshly retrieved page. The GetAllResponseHeaders method returns a string in which all the header/ value pairs are concatenated and each pair is separated from the others by a carriage-return character. The call

MsgBox obj.GetAllResponseHeaders

returns the output in Figure 1 when you run it under Win2K and Microsoft Internet Information Services (IIS) 5.0.

Where's the XML?
So far, I've talked only about XmlHttpRequest's HTTP capabilities. But as you certainly noticed, "XmlHttpRequest" begins with "XML." XmlHttpRequest has a powerful XML-related feature: It knows how to manipulate an XML Document Object Model (XMLDOM) over the Web. Essentially, XmlHttpRequest takes care of serializing and deserializing XMLDOM objects over an HTTP call.

You can send an XMLDOM to a server page for further processing. You can use the ASP Request object to automatically import the XMLDOM data to a variable, as the code in Listing 4 shows. After the ASP script processes the XML code, the script can set the content type to text/xml and return well-formed XML. You can then use XmlHttpRequest's ResponseXml property to expose the XMLDOM, as in

xmlhttp.send ""
Set xmldom = xmlhttp.ResponseXml
MsgBox xmldom.xml

You assign the property to a variable, then use the variable as usual. Notice that after you set the variable, the xmlhttp object is a valid, but empty, XMLDOM object.

Microsoft introduced XmlHttpRequest as part of IE 5.0 more than 2 years ago. You can now obtain it as part of Win2K and MSXML 3.x. The object lets you communicate between scripts and HTTP servers and even exchange XML document objects. With XmlHttpRequest, you can issue a generic HTTP request to a Web server and use XML to send and receive data. The component also gives you a quick way to use simple VBScript code to download pages from the Web.