Building A SOAP Client With Visual C++ - SoapSerializer
(Page 3 of 5 )
The SoapSerializer object is used to build a SOAP message to be sent to the service. The SoapSerializer object must be connected with the SoapConnector object before communicating with the server. To interconnect these two objects, we need to call the Init method of the SoapSerializer object. This method takes a single argument, which is the InputStream (the stream the sends data to the server):
// Creating a SoapSerializer object and initializing it with InputSTream
ISoapSerializerPtr Serializer; Serializer.CreateInstance(_uuidof(SoapSerializer)); Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));Before looking into other functions of SoapSerializer, let's take a look at a sample SOAP request to get an idea of what we are building in our code:
<SOAP: Envelope xmlns:SOAP="soap namespace">
<SOAP:Body>
<m:someMethodName xmlns:m="some namespace">
<someParameter> someParameterValue </someParameter>
<m:someMethodName>
</SOAP:Body>
</SOAP: Envelope>A soap request is simply encapsulated into tags. The <Envelope> tag is the main tag of this SOAP Document. A SOAP message is always encapsulated in an envelope. The envelope contains a message body, which is specified by a <Body> tag. The body contains the actual request. In C++, we have the appropriate methods to create these tags and specify any values for them. The following code demonstrates the use of these methods:
Serializer->startEnvelope("SOAP","","");
// begins an element in a SOAP message, first argument defines the namespace, if it
// is empty then SOAP-ENV is used by default, the second and the third argument defines the URI
// and the Encoding Type respectively Serialzier->startBody("");
// begins the <Body> element in the message, the first argument defines the encoding style Uri,
// by default it is NONE.
Serializer->startElement("someMethodName","","","m");
// Begins a child element into the body element of a SOAP message, the first parameter is the
// element name, the second parameter is the Uri, third is encoding style and the last element
// is the namespace for the element.
Serializer->WriteString("someParameterValue")
// writes the value of an element.All the functions shown above that start with startXXX have their equivalent endXXX function to end the element as well. After finishing with the message, the connector's endMessage() method is called to actually send the message as described above.
We have now connected with the service, prepared our request and sent it to the service. The next and the final step is to read the response from the server, which is what we will look at now.
SoapReaderThis object reads the response from the service and parses the incoming message into the DOM for further processing. Following is a sample SOAP Response from the service:
<SOAP: Envelope xmlns:SOAP="soap namespace">
<SOAP:Body>
<m:someMethodNameResponse xmlns:m="some namespace">
<return> someResult </return>
<m:someMethodNameResponse>
</SOAP:Body>
</SOAP: Envelope>Before calling any functions to get the result, we connect with the OutputStream to actually read the response in a SoapReader object (An OutputStream receives data from the service):
// code to create a SOAPReader object and connecting with the outputstream
ISoapReaderPtr Reader; Reader.CreateInstance(_uuidof(SoapReader)); Reader->Load(_variant_t((IUnknown*)Connector->OutputStream));
// the load method can also accept an XML Document File or StringAfter loading the response into our SoapReader object, we get the result by calling the RPCResult property of SoapReader object. But RPCResult doesn't return the actual result; it returns the first child element of the first entry in the <Body> element. We get the result by calling the text property:
Reader->RPCResult->textNext: Demonstrating a sample SOAP client >>
More C++ Articles
More By Nauman Laghari