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 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope" 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:Body>
    <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:Envelope>

This soap message contains three basic elements.

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

All the soap messages should be within the envelope element.

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

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

The "mustUnderstand" attribute in the header element will make this 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.

The body element has a method that 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.

Their datatype is 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 the processing of the message, a fault message is returned that is defined in the Fault tag within the body. This message is shown only in case of an error during processing. If there is any error during the transfer of the data or parameters to the web services normal HTTP error is shown. Any fault messages can have four sub-elements.

  1. fault code (optional): contains the type of error that occurred.
  2. fault string (optional): contains the cause of the error.
  3. faultactor: specifies the server name where the error occurred.
  4. details: contains two sub-elements.
  5. message: contains the detailed error message.
  6. error code: contains the error code.

After the successful execution of the Firstmethod web service, it will return a string

that is wrapped by XML tags. The firstMethodResponse element is responsible for returning the value.

Any response element will be in the form of the 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 the "FirstMethod" service.

Soap action specifies the unique URI of the web service invoked.


Similar Articles