Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

August 28, 2001 12:00 AM

VBScripting Solutions: The XmlHttpRequest Object

Windows IT Pro
InstantDoc ID #22043
Rating: (0)
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.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.