Soap


As mentioned, one method used to exchange data with Web services is SOAP. This technology has had a lot of press, especially since Microsoft decided to adopt it for use in the .NET Framework. Now, though, the excitement seems to be dying down a bit as the SOAP specification is finalized. When you think about it, finding out exactly how SOAP works is a bit like finding out about how HTTP works — interesting, but not essential. Most of the time you don't have to worry about the format of the exchanges made with Web services; they just happen, you get the results you want, and everyone is happy.

For this reason, this section won't go into a huge amount of depth, but you will see some simple SOAP requests and responses so you can get a feel for what is going on under the hood.

Imagine that you want to call a method in a Web service with the following signature:

 int DoSomething(string stringParam, int intParam) 

The SOAP headers and body required for this are shown in the following code, with the address of the Web service (more on this later) at the top:

 POST /SomeLocation/myWebService.asmx HTTP/1.1 Host: hostname Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/DoSomething" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <DoSomething xmlns="http://tempuri.org/">          <stringParam>string</stringParam>          <intParam>int</intParam> </DoSomething> </soap:Body> </soap:Envelope> 

The length parameter here specifies the total byte size of the content and will vary depending on the values sent in the string and int parameters. Host will also vary, depending on where the Web service is located.

The soap namespace referenced here defines various elements that you use to build your message. When you send this over HTTP, the actual data sent is slightly different (but related). For example, you could call the preceding method using the simple GET method:

 GET /SomeLocation/myWebService.asmx/DoSomething?stringParam=string&intParam=int HTTP/1.1 Host: hostname 

The SOAP response of this method is as follows:

 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <DoSomethingResponse xmlns="http://tempuri.org/">          <DoSomethingResult>int</DoSomethingResult> </DoSomethingResponse> </soap:Body> </soap:Envelope> 

where length varies depending on to the contents, in this case int.

The actual response over HTTP is simpler, as shown in this example:

 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0"?> <int xmlns="http://tempuri.org/">int</int> 

This is a far simpler XML format.

As discussed at the start of this section, the beauty of all this is that you can ignore it completely. Only if you want to do something really odd does the exact syntax become important.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net