SOAP, or Simple Object Access Protocol, is the backbone of web services and a variety of new technologies. In this article Nauman shows us how to create a simple SOAP client with Visual C++. He describes several methods found in the SOAP toolkit, and finishes off by creating a SOAP client that checks whether or not a Yahoo user is currently online.
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
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.
SoapReader This 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:
// the load method can also accept an XML Document File or String
After 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: