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 methodsOpen and Sendplay 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 operationit 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.