In the last tutorial we saw , what is web service , what is the use of web services and different jargon's of web service.such as SOAP,WSDL ,UDDI.
In this tutorial we will be seeing in detail about SOAP.
SOAP :
- Soap is a XML -Based Protocol for exchanging information between computers.
- It can be used in variety of Messaging System and can be delivered in variety of Transport Protocols.
- It is Platform Independent.
- Soap can be implemented in any technologies such as Java,.NET,C++,Perl etc.
Soap Specification has "3" Major Parts.
1. Soap Envelope
2.Data Encoding Rules
3. RPC Conventions
1. Soap Envelope : A Soap Envelope contains the specific rules for encapsulating data being transferred between computers.This includes Application-Specific data ,such as Method name to invoke,Method Parameters and return types.
2. Data Encoding Rules:
To exchange data, computers must agree on rules for encoding specific data types.For example, two computers that process stock quotes need an agreed-upon rule for encoding float data types; likewise, two computers that process multiple stock quotes need an agreed-upon rule for encoding arrays. SOAP therefore includes its own set of conventions for encoding data types. Most of these conventions are based on the W3C XML Schema specification.
3. RPC Conventions :
SOAP can be used in a variety of messaging systems, including one-way and two way messaging. For two-way messaging, SOAP defines a simple convention for representing remote procedure calls and responses. This enables a client application to specify a remote method name, include any number of parameters, and receive a response from the server.
Example :
A simple soap example which has the method "Temperature " and passes the zip code in the request and gets the temperature in Response.
Request :
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getTemp
xmlns:ns1="urn:xmethods-Temperature"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<zipcode xsi:type="xsd:string">10016</zipcode>
</ns1:getTemp>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In this example couple of things have to be notice :
- The first line is the XML declaration with version and encoding.
- The second line contains mandatory "Soap Envelope"which has the details of all information which we need to pass.
- The Envelope contains different namespace which are required for making a soap request.
- The next line we have Soap body tag which again a mandatory data element.
- Next we have the Method name "Temperature" and parameter zip-code as String.
Note : We don't exactly need to know every thing happens in soap, but basic understanding is important. So when we request a service from any language we just access the method and pass the value behind the scenes , request will formed in this manner and send to the service.
Response :
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getTempResponse
xmlns:ns1="urn:xmethods-Temperature"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:float">71.0</return>
</ns1:getTempResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As we can see , a response with float return type of Temp is returned.From this Response we have to extract the data. This is how the request and response happens in real world.
But, as i said when we access a web service from languages such as java,c++ etc we don't really see what is happening in background we formed the request using various web service API's and framework and extract the response by the same way.
A Soap Message basically contains the following elements
1. Envelope - Mandatory
2. Headers - Optional
3. Body - Mandatory
4. Fault - Optional
1. Envelope :
- It is a root element of SOAP Messages.
- Soap uses XML namespaces to differentiate versions.
Ex:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
The SOAP 1.1 namespace URI is http://schemas.xmlsoap.org/soap/envelope/, whereas the SOAP 1.2 namespace URI is http://www.w3.org/2001/09/soap-envelope.If the Envelope is in any other namespace, it is considered a versioning error.
2. Headers :
- It is an optional element is Soap Messaging.
- The optional Header element offers a flexible framework for specifying additional
application-level requirements.
application-level requirements.
- For example, the Header element can be used to specify a digital signature for password-protected services.
-The Header framework provides an open mechanism for authentication, transaction management, and payment authorization.
It has "2" Main Attributes :
- Actor
- Must understand Attribute
Actor attribute:
The SOAP protocol defines a message path as a list of SOAP service nodes. Each of these intermediate nodes can perform some processing and then forward the message to the next node in the chain. By setting the Actor attribute, the client can specify the recipient of the SOAP header.
MustUnderstand attribute :
Indicates whether a Header element is optional or mandatory. If set to true , the recipient must understand and process the Header attribute according to its defined semantics, or return a fault.
SOAP 1.1 uses integer values of 1/0 for the MustUnderstand attribute; SOAP 1.2 uses Boolean values of true/1/false/0.
The Header specifies a payment account, which must be understood and processed by the SOAP server.
Here is an example Header :
<SOAP-ENV:Header>
<ns1:PaymentAccount xmlns:ns1="urn:ecerami" SOAP-ENV: mustUnderstand="true">
orsenigo473
</ns1:PaymentAccount >
</SOAP-ENV:Header>
<ns1:PaymentAccount xmlns:ns1="urn:ecerami" SOAP-ENV: mustUnderstand="true">
orsenigo473
</ns1:PaymentAccount >
</SOAP-ENV:Header>
3. Body :
The Body element is mandatory for all SOAP messages. As we have already seen, typical
uses of the Body element include RPC requests and responses.
uses of the Body element include RPC requests and responses.
<SOAP-ENV:Body>
<ns1:getTemp
xmlns:ns1="urn:xmethods-Temperature"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<zipcode xsi:type="xsd:string">10016</zipcode>
</ns1:getTemp>
</SOAP-ENV:Body>
4. Fault :
- It is an optional element.
- In the event of an error, the Body element will include a Fault element.
- The following code is a sample Fault. The client has requested a method named
ValidateCreditCard , but the service does not support such a method.
ValidateCreditCard , but the service does not support such a method.
This represents a client request error, and the server returns the following SOAP response:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode>
<faultstring xsi:type="xsd:string">
Failed to locate method (ValidateCreditCard) in class
(examplesCreditCard) at /usr/local/ActivePerl-5.6/lib/
site_perl/5.6.0/SOAP/Lite.pm line 1555.
</faultstring>
</SOAP-ENV:Fault></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode>
<faultstring xsi:type="xsd:string">
Failed to locate method (ValidateCreditCard) in class
(examplesCreditCard) at /usr/local/ActivePerl-5.6/lib/
site_perl/5.6.0/SOAP/Lite.pm line 1555.
</faultstring>
</SOAP-ENV:Fault></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP Fault codes :
SOAPENV: VersionMismatch
Indicates that the SOAP Envelope element included an invalid namespace, signifying a version mismatch.
Indicates that the SOAP Envelope element included an invalid namespace, signifying a version mismatch.
SOAPENV:MustUnderstand
Indicates that the recipient is unable to properly process a Header-element with a must-understand attribute set to true. This ensures that must-understand elements are not silently ignored.
Indicates that the recipient is unable to properly process a Header-element with a must-understand attribute set to true. This ensures that must-understand elements are not silently ignored.
SOAP-ENV:Client
Indicates that the client request contained an error. For example,the client has specified a nonexistent method name, or has supplied the incorrect parameters to the method.
Indicates that the client request contained an error. For example,the client has specified a nonexistent method name, or has supplied the incorrect parameters to the method.
SOAP-ENV:Server
Indicates that the server is unable to process the client request. For example, a service providing product data may be unable to connect to the database.
Indicates that the server is unable to process the client request. For example, a service providing product data may be unable to connect to the database.
-------------------------------------------------------------------------------------------------
In the next part , we will see soap encoding and WSDL details.....Please comment your thoughts.

blogs