Listing 5: Sample use of the author’s SOAP utility classes

//
// AddNews SOAP Client Application
//
//  Written by Chris Dix, July 2000
//  Don't drop the SOAP.
 
#include <stdio.h>
#include <string.h>
#include <iostream.h>

#include <winsock2.h>   // Windows socket functions
#include "soaputil.h"   // SOAP Utility functions
using namespace SoapUtils;

// SOAP-Web Services Resource Center
const char* kSOAPWRCHost =      "www.soap-wrc.com";
const char* kSOAPWRCAddress =   "/webservices/soapv11.asp";

// Microsoft
const char* kMSHost =           "131.107.91.76";
const char* kMSAddress =        "/soapdemo/services.asp";

bool AddNews( const string& strLogin, 
    const string& strPassword,
    const string& strCaption, const string& strDescription,
    const string& strURL, string& strID )
{
        SimpleSoapRpcMessage msg;
        SimpleSoapRpcMessage response;
    
    strID.erase();

    msg.putMethod( "AddNews" );
    msg.putMethodNamespace( 
        "http://www.soap-wrc.com/webservices/soapv11.asp", 
        "isoapwrc" );

    msg.addParameter( "login", strLogin );
    msg.addParameter( "password", strPassword );
    msg.addParameter( "caption", strCaption );
    msg.addParameter( "description", strDescription );
    msg.addParameter( "url", strURL );
    
    SoapHttpPostTransport binding;
    binding.putHost( kSOAPWRCHost );
    binding.putAddress( kSOAPWRCAddress );
    binding.debugging( true );

    bool bSuccess = binding.sendMessage( &msg, &response );
    if ( bSuccess )
    {
        if ( response.containsFault() )
        {
            cout << "The response contained a fault.\n";
            string s = response.getBody();
            cout << s.c_str();
            return( false );
        }
        strID = response.getReturnValue();
    }

    return( bSuccess );
}

bool GetStockQuote( const string& strSymbol, 
    const string& strDescription, string& strPrice )
{
        SimpleSoapRpcMessage msg;
        SimpleSoapRpcMessage response;
    
    strPrice.erase();

    msg.putMethod( "GetStockQuote" );
    msg.putMethodNamespace( 
        "http://www.soaptoolkit.com/soapdemo/services.xml", 
        "ss" );
    
    msg.addParameter( "symbol", strSymbol );
    msg.addParameter( "description", strDescription );
    
    SoapHttpPostTransport binding;
    binding.putHost( kMSHost );
    binding.putAddress( kMSAddress );
    binding.debugging( true );

    bool bSuccess = binding.sendMessage( &msg, &response );
    if ( bSuccess )
    {
        if ( response.containsFault() )
        {
            cout << "The response contained a fault.\n";
            string s = response.getBody();
            cout << s.c_str();
            return( false );
        }
        strPrice = response.getReturnValue();
    }

    return( bSuccess );
}

int main( void )
{    
    bool bSuccess = false;
    char pszHostName[50];
    string strCaption;
    string strDesc;
    string strHostName;
    string strID;
    string strPrice;

    cout << "C++ Users Journal SOAP Client Example\n";
    
    // Make a call to the Microsoft example site.
    bSuccess = GetStockQuote( "MSFT", 
        "Microsoft Corporation", strPrice );
    if ( bSuccess )
    {
        cout << "GetStockQuote succeeded, price is $";
        cout << strPrice.c_str();
        cout << ".\n";
        strDesc = "Stock Quote for Microsoft(MSFT): $";
        strDesc += strPrice;
        strDesc += ".";
    }
    else
    {
        cout << "Call to GetStockQuote failed.\n";
        strDesc = "Could not access stock quote server.\n";
    }

    // Try to get the name of the local machine.
    memset( pszHostName, 0, 50 );
    if ( gethostname( pszHostName, 50 ) == SOCKET_ERROR )
    {
        cout << "Could not get the client host name.\n";
        strHostName = "Unknown";
    }
    else
    {
        strHostName = pszHostName;
    }

    strCaption = "Message sent by ";
    strCaption += pszHostName;

    // Call the SOAP-WRC site, and we pass in the results 
    // of our call to Microsoft.  With that, we have 
    // combined two Web Services.
    bSuccess = AddNews( "cuj", "stroustrup", strCaption,
        strDesc, "http://www.cuj.com/", strID );

    // Uncomment these lines if you want to see a response
    // that contains a SOAP Fault.
    //bSuccess = AddNews( "cujo", "stroustrup", strCaption,
    //    strDesc, "http://www.cuj.com/", strID );

    if ( bSuccess )
    {
        cout << "AddNews succeeded, id is ";
        cout << strID.c_str();
        cout << ".\n";
    }
    else
    {
        cout << "Call to AddNews failed.\n";
    }

    return( 0 );
}
— End of Listing —