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.
The Power of the XMLHTTP Library - Processing the request on the remote server (Page 3 of 5 )
Once a client has submitted a request to a remote server, that server is in control of processing the request and providing meaningful data, which can then be returned to the client. To demonstrate this, lets create two ASP pages: The first will act as the client. The second will process the client’s request and return a string of data.
Our first page will use the “GET” HTTP method, as well as one name/value pair tacked onto the end of the URL. The name/value pair “fName=Mitchell” contains my first name. The second page will use this name/value pair to return my complete name to the client, in the form of XML. The client page is named client.asp and its code looks like this:
Response.ContentType = "text/xml"
set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
The second page, server.asp which will extract the name/value pair, fName and return my complete name as a string, looks like this:
if Request.ServerVariables("HTTP_METHOD") = "GET" then
fName = Request.QueryString("fName")
Select Case fName
Response.Write fName & " Harper"
Response.Write fName & " Doe"
Response.Write fName & " Potter"
The server.asp script starts of by checking if the HTTP request method is set to “GET”. If it is, then we extract the value of the fName name/value pair from the query string. Next, we output the start of our XML document, simply using a case statement to determine which name is being requested. You can change the name in client.asp to “John”, “Harry” or anything else you like to see the different results.
Here is the XML returned from the server.asp page when the client.asp page is ran in my browser with fName=Mitchell:
Our server.asp page doesn’t use any fancy techniques to return the XML to the client.asp page: it uses the same Response.Write method that is available through ASP. The pages buffer (which is created using the Response.Write method) is returned to the client.asp page as the response data.