Home arrow C++ arrow Page 3 - Building A SOAP Client With Visual C++

Building A SOAP Client With Visual C++

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.

Author Info:
By: Nauman Laghari
Rating: 4 stars4 stars4 stars4 stars4 stars / 35
April 29, 2002
  1. · Building A SOAP Client With Visual C++
  2. · Fundamentals of SOAP Programming
  3. · SoapSerializer
  4. · Demonstrating a sample SOAP client
  5. · Conclusion

print this article

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">
<m:someMethodName xmlns:m="some namespace">
<someParameter> someParameterValue </someParameter>
</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:

// 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.

// 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.

// 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.

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:

<SOAP: Envelope xmlns:SOAP="soap namespace">
<m:someMethodNameResponse xmlns:m="some namespace">
<return> someResult </return>
</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 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:

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials