Tutorials » The Power of the XMLHTTP Library

Processing the request on the remote server

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"

dim objXMLHTTP
set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")

objXMLHTTP.Open "GET", "http://localhost/server.asp?fName=John", false
objXMLHTTP.SetRequestHeader "Content-type", "text/xml"
objXMLHTTP.Send

Response.Write objXMLHTTP.ResponseText

%>

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

dim fName
fName = Request.QueryString("fName")

Response.Write "<Name_Details>"
Response.Write "<TheName>"

Select Case fName
Case "Mitchell"
Response.Write fName & " Harper"
Case "John"
Response.Write fName & " Doe"
Case "Harry"
Response.Write fName & " Potter"
Case Else
Response.Write "[Unknown]"
End Select

Response.Write "</TheName>"
Response.Write "</Name_Details>"

end if
%>

The server.asp script starts off 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 run in my browser with fName=Mitchell:

XML values displayed in asp page

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.

Previous Start Next
 
  1 2 3 4 5