Home arrow ASP arrow Page 2 - The Power of the XMLHTTP Library
ASP

The Power of the XMLHTTP Library


XMLHTTP is a set of API's that allows us to send and retrieve data to and from a remote web server using its underlying HTTP protocols and methods. In this article, Mitchell provides us with a basic introduction to the XMLHTTP library, with some examples to get you started.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 76
December 18, 2001
TABLE OF CONTENTS:
  1. · The Power of the XMLHTTP Library
  2. · XMLHTTP in a nutshell
  3. · Processing the request on the remote server
  4. · The XMLHTTP document headers
  5. · Conclusion

print this article
SEARCH DEVARTICLES

The Power of the XMLHTTP Library - XMLHTTP in a nutshell
(Page 2 of 5 )

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:

<html>

<head>

<title> XMLHTTP Example </title>

</head>

<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"

objXMLHTTP.Send



%>

<xmp>

<%=objXMLHTTP.ResponseText%>

</xmp>

</body>

</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. Pretty neat, ey?

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.
blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials