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 propertiesResponse-Stream and ResponseBodyformat 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.