Building A SOAP Client With Visual C++ - Demonstrating a sample SOAP client
(Page 4 of 5 )
For demonstrating the use of the SOAP classes that we've looked at in this article, I've used one of the services listed on
http://www.xmethods.net/. The service indicates Yahoo Messenger's online presence. You can find the required details by following
this URL. The only thing it expects is a method parameter i.e. the Yahoo user's login id. The result returned is a boolean value indicating 0 for offline and 1 for online. Other details are available on the site or by viewing the WSDL
here.
Anyway, I guess the best way to learn something is to see the source code in action, so that's exactly what we will do now. Here's the C++ code for a console application that uses SOAP calls to find out if a Yahoo user is online:
#include
#import "msxml3.dll"
using namespace MSXML2;
#import "C:\Program Files\Common Files\MSSoap\Binaries\MSSOAP1.dll" \
exclude("IStream", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib;
void main()
{
CoInitialize(NULL);
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service
Connector.CreateInstance(__uuidof(HttpConnector));
Connector->Property["EndPointURL"] = "http://www.allesta.net:51110/webservices/soapx4/isuseronline.php";
Connector->Connect();
// Begin message
Connector->Property["SoapAction"] = "uri:allesta-YahooUserPing";
Connector->BeginMessage();
// Create the SoapSerializer
Serializer.CreateInstance(__uuidof(SoapSerializer));
// Connect the serializer to the input stream of the connector
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// Build the SOAP Message
Serializer->startEnvelope("","","");
Serializer->startBody("");
Serializer->startElement("isuseronline","uri:allesta-YahooUserPing","","m");
Serializer->startElement("username","","","");
Serializer->writeString("laghari78");
Serializer->endElement();
Serializer->endElement();
Serializer->endBody();
Serializer->endEnvelope();
// Send the message to the web service
Connector->EndMessage();
// Let us read the response
Reader.CreateInstance(__uuidof(SoapReader));
// Connect the reader to the output stream of the connector
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
// Display the result
printf("Answer: %s\n", (const char *)Reader->RPCResult->text);
CoUninitialize();
}As you can see, the code is fairly straight forward, and even if you haven't worked with C++ before, then I'm sure that you can figure out what the code does: Firstly, it connects to the remote server. Secondly, it creates the SOAP message and sends it. Lastly, it reads the response from the server and outputs it to the screen with printf.
Next: Conclusion >>
More C++ Articles
More By Nauman Laghari