Tutorials » The Power of the XMLHTTP Library

XMLHTTP in a nutshell

XMLHTTP can be thought of as an XML-based response/request protocol, which allows us to send text/xml to a remote server. That server processes the request, and in-turn, returns either a text/XML data stream or a binary stream of characters. There are literally an unlimited number of ways that the XMLHTTP library can be used. Here’s a simple ASP example to get you started:

<title> XMLHTTP Example </title>
<body bgcolor="#FFFFFF" text="#000000">
dim objXMLHTTP
set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")

objXMLHTTP.Open "GET", "http://www.devarticles.com/books.php", false
objXMLHTTP.SetRequestHeader "Content-type", "text/html"


In the example above, we’re creating a new instance of the XMLHTTP library. The XMLHTTP library’s ProgID is Microsoft.XMLHTTP. Next, we call its Open method, which takes three arguments:

  1. Method: The method argument tells the XMLHTTP library which protocol we will be using to communicate with the remote server. The XMLHTTP library is compatible with the HTTP 1.1 protocol.
  2. URL: Tells the XMLHTTP library the location and page of the remote server where all data should be sent. The URL argument can also include name/value pairs, such as http://www.mysite.com/getbook.asp?bookId=345&authorId=953
  3. Async: Tells the XMLHTTP library whether to use asynchronous transfer mode or not. If true, the XMLHTTP library will handle all communication and data transfer in the background, allowing your ASP page to continue to load. We have set it to false, because we want our data available before continuing (AKA synchronous mode).

There are also two other arguments that the Open method of the XMLHTTP library accepts. These are the username and password, which should be tacked onto the end of any call to the Open method if you’re requesting data that is on a server needing authentication details passed with the transmission. Most of the time, these will not be included.

The SetRequestHeader method allows us to set the headers that will be passed along with our XML data to the remote web server. Next, we call the Send method, which actually carries out the request on the remote server.

Because we have chosen to use synchronous transfer methods, our data is available as soon as the Open method is complete. The ResponseText variable contains the resultant response from the remote server as plain text. If we were receiving binary data, we would use the ResponseBody variable. As you can see, we have outputted the resultant text inside of <xmp> and </xmp> tags, which tell the browser to render anything between those tags as text, and not HTML.

Go ahead and copy the code above into an ASP page on your server. It will retrieve the code behind the devArticles.com recommended reading page. If you remove the <xmp> and </xmp> tags, you will see the HTML rendered version of the text retrieved from the Send method. Yes, the XMLHTTP library has literally contacted the devArticles.com server, retrieved the code behind books.php and stored it in the ResponseText variable of our XMLHTTP object.

The XMLHTTP object doesn’t stop there, however. Let’s now see an example where the remote server processes the clients request and returns some sort of meaningful response.

Previous Start Next
  1 2 3 4 5