Remote Procedure Calls using SOAP

Here is the sample RPC (remote procedure call) using SOAP ( simple object access protocol). A SOAP message is an XML format sent over HTTP to a remote server where the Web service is located.

The Web service processes the soap request and returns the value to the client using soap response.

POST/ FirstSoap.aspx HTTP/1.1
Host: www.anyserver.com
Content-Type: text/xml; charset="utf-8"
Content-Length:888
SOAPAction: "uri:uniqueuriofthewebservice"
<SOAP-ENV:Envelope
<!-- namespace for envelope -->
xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope">
<!-- namespace for xml schema specification ( for data type declarations) -->
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header>
<
MYHEAD:USERID
xmlns
:MYHEAD="URI"
SOAP-ENV:mustUnderstand="1">
user email id
</MYHEAD:USERID>
</SOAP-ENV:HEADER>
<
SOAP-ENV:Body xmlns:BODYNS="URI1">
<
BODYNS:FirstMethod>
<
parm1 xsi:type="int">1</parm1>
<
parm2 xsi:type="string">Ranjith</parm2>
<
parm3 xsi:type="string">Rajadurai</parm3>
</
BODYNS:FirstMethod>
<
SOAP-ENV:Fault>
<
faultcode>server</faultcode>
<
faultstring>application error</faultstring>
<
faulterror>server name</faultactor>
<
detail xmlns:Faultns="URI2">
<
Faultns:Message>Wrong number of parameters</Faultns:Message>
<
Faultns:ErrorCode>1001</Faultns:ErrorCode>
</
detail>
</
SOAP-ENV:Fault>
<
BODYNS:FirstMethodResponse>
<
return>My First RPC responded successfully</return>
</
BODYNS:FirstMethodResponse>
</
SOAP-ENV:Body>
</
SOAP-ENV:Envelope>

This soap message contains three basic elements

  1. Envelope
  2. Header (optional)
  3. Body

All the soap message should be within the envelope element.

Each element should have namespace for scoping. Member of the element can be accessed within that namespace.Namespace is required to avoid confusion incase of having same element names in different places within a message.

In this soap message header element passes the user email id for authentication.

"mustUnderstand" attribute in the header element will make this as mandatory i.e the web service that is going to process this soap message must understand the parameter passed in that header element.

The flag is set to "1" to make this authentication as mandatory. Now the soap message is authenticated before processing. Otherwise, it will not process the soap message.

Body element has a method which is specified in the WSDL of the web service.

This method can be defined in any language in the web service.This method contains three parameters.

There datatype also defined using the namespace "xsi". If this soap message is correctly defined it will invoke the FirstMethod web service and return a value.

If there is any error during processing the message, a fault message is returned that is defined in Fault tag within the body. This is message is shown only incase of error during processing. If there is any error during transfering the data or parameters to the web services normal HTTP error is shown.Any fault messages can have four sub elements.

  1. faultcode(optional) - contains the type of error occured.
  2. faultstring(optional) - contains the cause of the error.
  3. faultactor - specifies the server name where the error is occurred.
  4. details - contains two sub elements.
    a. message - contains the detail error messge
    b. errorcode - contains the error code.

After successful exceution of the Firstmethod web service it will return a string
which is wrapped by XML tags. FirstMethodResponse element is responsible for returning the value.

Any response element will be in the form of method name suffix "Response".

This soap message is sent in the form of HTTP packets to the url www.anyserver.com/FirstSoap.aspx Web service invoked is "FirstMethod" service.
Soap action specifies the unique URI of the web service invoked.


Similar Articles