ASP.NET, SQL Server, WPF, C#, VB.NET
Articles
About Us
Web Services - Server To Server With XMLHTTP
By Robbe D. Morris
Printer Friendly Version
Robbe & Melisa Morris
The concept of web services has been out there for awhile now and is starting to catch on. Those of us who are used to standard web development practices for accessing relational databases might not see new opportunities that are right in front of us. Such is the case with XML. This article is designed to spark an interest in the server to server transfer of data arena. It is important to note that this example does not implement SOAP specifically. While the true definition of a web service does not require the use of SOAP, it is fast becoming one of the standard methods for accessing web services. If you would like to read more,
MSDN Web Services Defined
I'd encourage you to do so.
Article Update - June 24, 2001 -
Web Services - Server To Server With SOAP
.
Related Article - August 22, 2001 -
Insert Records - XML/ADO Marshalling Over The Net
.
Related Article - September 6, 2001 -
Single SOAP Message / Multiple ADO Recordsets
.
Today, we'll just get you started with a bare bones example of web service source code needed for both the client side (server A) and server side (server B). In the following code samples, we'll assume that both server A and server B are Windows 2000 servers running IIS 5.0 and also have the latest Microsoft XML Parser (3.0) and SDK installed. When you view the server B code, take notice that it does not require the Microsoft XML toolset. We'll show ADO as an example but you can place your database access code according to the platform that exists on the server. All references to "client side" code actually mean the code is running on server A in ASP and not the visitor's browser. Performing similar functionality via the visitor's browser is another article for another day. Let's dig in...
Client Side (Server A or serverA.asp)
The client side code needs to access a remote system to retrieve data. In this case, we want to retrieve an ADO recordset from a remote server. To accomplish this, we'll ask the server B web service to reply with an XML data stream formatted specifically to allow us to load the server A ADO recordset with the XML data stream. It is important to note at the time of this writing, ADO required specially formatted XML files/streams in order to load the data into an actual recordset. It does not support loading of standard XML files. With this in mind, we'll inform the server B web service that we want the data returned formatted for use with an ADO recordset. In your usage of this process, you may want the XML stream returned in straight XML for use with XSL for eventual output to the user's browser. Let's look at the code:
<% Function GetXML() Dim oXMLHttp Dim oADORec Dim sXML Dim URL Set oXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP") URL= "http://www.yourdomainname.com/ServerB.asp?XMLREQUEST=1" oXMLHttp.open "GET", URL, false oXMLHttp.send() ' Send the request. if oXMLHttp.status = 200 Then sXML = oXMLHttp.responseText ' Retrieve from serverB. if ucase(trim(mid(sXML,1,6))) <> "XMLERR" then GetXML = LoadXMLRecordSet(sXML) else GetXML = "Err: " & Trim(mid(sXML,7,40)) end if else GetXML = "Could not get XML data." end if set oXMLHttp = nothing end function Function LoadXMLRecordSet(sXMLStream) dim oADORec dim oXML dim oCol dim sH sH = "
RecordSet Results" sH = sH & "
" & vbcrlf Set oADORec = Server.CreateObject("ADODB.Recordset") set oXML = Server.CreateObject("MSXML2.DOMDocument") ' Load the formatted string into an XMLdom object. oXML.loadXML sXMLStream ' ADO supports a direct load from an XMLdom object. oADORec.Open oXML ' Iterate through the recordset to see what we've got. While not oADORec.EOF for each oCol in oADORec.Fields sH = sH & "
" & oCol.Name & "
" sH = sH & "
" & oCol.Value & "
" next sH = sH & "
" oADORec.MoveNext wend Set oADORec = nothing Set oXML = nothing LoadXMLRecordSet = sH End function ' Let's write the results to the browser. response.write "Getting XML...
" response.write "
" response.write GetXML() response.write "
" response.write "" %>
Server Side (Server B or serverB.asp)
The server side code will look very similar to pages you've written before. Here, we'll look at the XMLREQUEST querystring value to determine whether the client side request wants an ADO formatted XML stream or a standard XML stream. Once the XML stream is created, we simply write the content back to the browser as we normally would. In our case, the output will actually be received by server A instead of the visitor's actual browser. You'll want to develop your own standard for returning errors. In this example, we use "XMLERR" as a flag in the first part of the stream to indicate we weren't successful. Your implementation will likely be much more advanced. Let's look at the code:
<% dim moADOCon dim moADORec dim msADOConStr ' Your database connection string. msADOConStr = Application("sDBConn") Sub ReturnXML() dim sXML dim sXMLType ' Your system's object for indicating content type (if applicable). Response.ContentType="text/xml" ' Your system's object to handle query strings. sXMLType = Trim(Request.QueryString("XMLREQUEST")) Select Case sXMLType Case "0" ' Return back straight XML. ' Use your database tool or business logic to ' create an XML stream. Then write your XML ' stream output just as you would sending it ' to the browser. In ASP, it would look like this: sXML = sXML & "" sXML = sXML & "
" sXML = sXML & "
" sXML = sXML & "
1
" sXML = sXML & "
Company 1
" sXML = sXML & "
" sXML = sXML & "
" sXML = sXML & "
2
" sXML = sXML & "
Company 2
" sXML = sXML & "
" sXML = sXML & "
Response.write sXML Case "1" ' Return back XML formatted specifically for ADO Set moADOCon = Server.CreateObject("ADODB.Connection") Set moADORec = Server.CreateObject("ADODB.Recordset") moADOCon.open msADOConStr Set moADORec.ActiveConnection = moADOCon ' ADO supports writing directly to the ASP Response ' object from the ADO recordset. moADORec.Open "Your SQL Statement Goes Here" moADORec.Save Response,adPersistXML On Error Resume Next If moADORec.STATE = adStateOpen Then moADORec.Close If moADOCon.STATE = adStateOpen Then moADOCon.Close Set moADORec = Nothing Set moADOCon = Nothing Case Else ' Your system's object for output to the browser ' (our server A). Response.write "XMLERR " & "0" end select end sub Call ReturnXML() %>
Now that you know how build a web service of your own, you can begin to consider ways in which this might benefit you. Perhaps you want to designate a specific application server to interface with all of your different relational database servers. Most operating systems (even the old ones) support HTTP and have (or easily could have) a web server process running on them. Your company may have old legacy code/processes for accessing data that could be greatly enhanced or web enabled through your existing web architecture. Whatever your situation, give XML based web services a second look as a potential solution to your needs.
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of EggHeadCafe which provides .NET articles, book reviews, software reviews, and software download and purchase advice.