Article
.NET Web Services Made Easy
Interested in .NET? Don't miss SitePoint's .NET Feature Guide -- it's an excellent resource!
No doubt you've heard of Web Services, even if you're an ASP, PHP, Java, .NET or XML developer. If you haven't, or don't really know what they are, have a look at Kevin Yank's "Web Services Demystified".
Web Services are neither a new concept, nor a Microsoft creation. The idea has been around for a long time -- Netscape touched on the concept with their IIOP (Internet Inter-ORB Protocol) -- and was discussed in an article by the cofounder of Netscape, Marc Andreessen, at the end of 1996.
In this article we'll explore how you can build and utilise your own Web Service.
Prerequisites
I'm assuming that you have a basic knowledge of .NET, Web forms, and accessing data. I'll use an MS SQL Server database for this project, but you can also use Microsoft MSDE, which comes with the .NET Framework (if you need help finding this, have a look at the SitePoint Forums), or edit the code to work with an Access database through the OLEDB Namespace.
Usable Protocols
Because Web Services are designed to be as accessible as possible, one of the primary and most important aspects of the architecture is to exchange data in a common format. You can use three protocols to transfer data in a .NET Web Service: Http-Get, Http-Post and SOAP (Simple Object Access Protocol). Let's go over these protocols now. Because Http-Get and Http-Post are essentially the same thing, we'll cover them together.
The Http-Get and Http-Post Protocols
Http-Get uses name/value pairs to pass data to and from a Web Service, and, as an HTTP-based protocol, it's widely accepted. You've probably worked with before if you've ever used the Request.QueryString() in ASP or ASP.NET. The Http-Get protocol appends a UUencoded string to the end of a URL, which commonly appears like this:
www.e4ums.com/search.aspx?searchtext=search%
20for%20this&from=index.aspx
Look familiar? Thought so.
Http-Post is also an HTTP-based protocol that uses name/value pairs, but instead of appending the URL, these pairs are passed in request header. If you've every used Request.Form(), then you've worked with Http-Post data. The actual HTTP header for an Http-Post request looks like this:
POST search.aspx HTTP1.1
Host: http://www.e4ums.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
searchtext=search%20for%20this&from=index.aspx
SOAP (Simple Object Access Protocol)
This lightweight data exchange protocol uses XML structure to describe objects and their properties and methods. Using HTTP and other Internet technologies, SOAP has seamlessly been integrated into .NET Web Services. Although only designed for one way transmission, SOAP messages can be combined to implement systems like request/response.
A SOAP message might look something like this:
POST search.aspx HTTP/1.1
Host: http://www.e4ums.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnn
SOAPAction: http://www.e4ums.com/ws
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:xsi=http://www.w3c.org/2001/XMLSchema-instance"
xmlns:xsd=http://www.w3c.org/2001/XMLSchema
xmlns:soap=http://schemas.xmlsoap.org/soap/envelope>
<soap:Body>
<tipOfDay xmlns=http://www.e4ums.com>
<date>05/12/2002</date>
</ tipOfDay >
</soap:Body>
</soap:Envelope>
The SOAP that is returned would look like this:
HTTP/1.1 200 OK
Content-Type: text/xml charset="utf-8"
Content-Length: nnnnn
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:xsi=http://www.w3c.org/2001/XMLSchema-instance"
xmlns:xsd=http://www.w3c.org/2001/XMLSchema
xmlns:soap=http://schemas.xmlsoap.org/soap/envelope>
<soap:Body>
<tipOfDay xmlns=http://www.e4ums.com>
<returnTip>Never stick a light build in your back
pocket</returnTip>
</tipOfDay>
</soap:Body>
</soap:Envelope>
Chris works as a Web Developer for a Scottish based web design firm, and is a partner at