Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / XML
Article

SOAP Client: Using SOAP Toolkit 3.0

Rate me:
Please Sign up or sign in to vote.
3.26/5 (16 votes)
2 Nov 2003 249.7K   2.9K   27   31
A SOAP client, using SOAP Toolkit 3.0.

Introduction

This article describes a project that provides a SOAP Client using SOAP Toolkit 3.0. It can be altered to use with SOAP Toolkit 2.0.

Using the code

The project is a Win32 console application. The user needs to change the method name as well as the path of the WSDL file.

Requirements

  • SOAP Toolkit 2.0 or 3.0
  • MSXML 4.0

Instructions to download as well as install these are present in the MSDN website.

//Code
HRESULT hRes = CoInitialize(NULL);

    ///////////////////NEW VERSION
    CString csMethodName="MEthodName";
    //OLECHAR FAR* fszMemberFunc[] = {L"MethodName"}; 
                    //Name of method being called


    LPOLESTR fszMemberFunc;
    IDispatch FAR* pdisp; //Pointer to IDispatch object
    DISPID dispid; //ID of method
    unsigned int uArgErr = 0;
    DISPPARAMS dispparams; //Parameter workarea
    EXCEPINFO excepinfo; //Exception Information
    HRESULT hresult; //return value, system functions
    VARIANT returnval; //return value(s) from server being called
    CString CEMessage;

    _bstr_t bstrWSDLFile
      (_T("http://xxx.xxx.xxx.xx/abcd/xyz/asaffsf.WSDL"));

    BSTR xmlInput;
    CString tempValue= "<TEST>Hi there</TEST>";
    xmlInput= tempValue.AllocSysString();



    dispparams.rgvarg = NULL;
//  CComPtr<ISOAPClient> m_ptrSoapClient;
    CComPtr<ISoapClient> m_ptrSoapClient;//For SOAPTOOLKIT 3.0
    try
    {
        hresult = AnsiToUnicode(csMethodName,&fszMemberFunc); 

        if(S_OK!=hresult)
        {
            CEMessage.Format("Unable to execute AnsiToUnicode"
                " for ProgID %s, with Error : %d",
                csMethodName, hresult);
            throw (CEMessage);
            //error converting the method anme
        }
        hresult=-1;
        //For SOAPTOOLKIT 2.0
    //  hresult = 
          m_ptrSoapClient.CoCreateInstance(__uuidof(SoapClient));
        //For SOAPTOOLKIT 3.0
        hresult = m_ptrSoapClient.CoCreateInstance(__uuidof(SoapClient30));
        if (S_OK != hresult)    {

            CEMessage.Format("CoCreateInstance failed for"
                   " SOAP CLIENT  with the Error : %d",
                    hresult);
            throw (CEMessage); //error creating an instance 
        }
        _bstr_t tmp(_T(""));
        hresult=-1;

        hresult = m_ptrSoapClient->MSSoapInit(bstrWSDLFile, 
                              "","","");//For SOAPTOOLKIT 3.0
//      hresult = m_ptrSoapClient->mssoapinit(bstrWSDLFile,
                              "","","");//For SOAPTOOLKIT 2.0

        if (S_OK != hresult) {

            CEMessage.Format("Initialization failed"
                    " for SOAP Client  with Error : %d", 
                    hresult);
            throw (CEMessage);
        }//Falied to inialize


        //    GetID of Member Function Being called
        hresult = -1;

        hresult = m_ptrSoapClient->GetIDsOfNames(IID_NULL, 
                    &fszMemberFunc, 1,    
                    LOCALE_SYSTEM_DEFAULT, &dispid);
        if (S_OK != hresult) {

            CEMessage.Format("GetIDsOfNames failed for"
              " ProgID SoapClient, Function Name %s,  with Error : %d", 
              fszMemberFunc,hresult);
            throw (CEMessage);
        }

        //Now setup arguments for transfer to server
        dispparams.rgvarg = new VARIANTARG[1];

        if (NULL == dispparams.rgvarg)
            throw ("Unable to allocate from the heap");

        dispparams.cArgs = 1;
        dispparams.cNamedArgs = 0;
        dispparams.rgdispidNamedArgs = NULL;
        dispparams.rgvarg[0].vt = VT_BYREF|VT_BSTR;
        dispparams.rgvarg[0].pbstrVal = &xmlInput;     

        //Setup initial conditions and Invoke Call
        VariantInit(&returnval);
        excepinfo.wCode = 0;
        excepinfo.bstrSource = NULL;
        excepinfo.bstrDescription = NULL;
        excepinfo.bstrHelpFile = NULL;
        hresult = -1;
        hresult = m_ptrSoapClient->Invoke(dispid, IID_NULL, 
                    LOCALE_USER_DEFAULT, DISPATCH_METHOD,
                    &dispparams, &returnval, &excepinfo, &uArgErr);


        if (S_OK != hresult) {
            if (DISP_E_EXCEPTION == hresult) {

                char src[256];
                char dsc[256];
                DWORD iLengthS = 
                   SysStringByteLen (excepinfo.bstrSource);
                DWORD iLengthD = 
                   SysStringByteLen (excepinfo.bstrDescription);
                WideCharToMultiByte(CP_ACP, NULL, 
                    excepinfo.bstrSource,iLengthS, 
                    src, sizeof(src), NULL, NULL);
                WideCharToMultiByte(CP_ACP, NULL, 
                    excepinfo.bstrDescription,iLengthD, 
                    dsc, sizeof(dsc), NULL, NULL);
                CEMessage.Format("exceptinfo: bstrSource '%s', 
                    bstrDescription '%s', wCode '%d'", 
                    src, dsc, excepinfo.wCode);
                //
            }

            bstr_t faultString=m_ptrSoapClient->GetFaultString();
            CString tempFaultString=(LPCSTR)faultString;
            bstr_t  detail=m_ptrSoapClient->GetDetail();
            CString tempfaultdetail=(LPCSTR) detail;
            CEMessage.Format("IDispatch.Invoke() failed for"
                 " ProgID SoapClient, Function Name %s  with HRESULT"
                 "   %d  FaultString %s and FaltDetails %s", 
                 csMethodName,hresult,tempFaultString,tempfaultdetail);
            throw (CEMessage);
        }

        CString temp= returnval.bstrVal;//temp has the results

        SysFreeString(xmlInput);;
    }
    catch (CString& csMsg)
    {
        SysFreeString(xmlInput);

    }
    catch (CException* pException)
    {
        SysFreeString(xmlInput);
        char msg[256];
        pException->GetErrorMessage(msg,sizeof(msg),NULL);

        delete pException;
    }
    catch (...)
    {
        SysFreeString(xmlInput);
    }

    if (m_ptrSoapClient)
    {
        m_ptrSoapClient.Release();
        pdisp = NULL;
    }
    if (NULL != dispparams.rgvarg)
        delete [] dispparams.rgvarg;
    CoUninitialize();

//

Points of interest

Couple of points which are worth mentioning:

  1. In case the user encounters this error:
    exceptinfo: bstrSource 'Connector', bstrDescription 
      'Connector:Connection time out. 
    HRESULT=0x800A1527 - Client:An unanticipated error occurred 
      during the processing of this request. 
    HRESULT=0x800A1527 - Client:Sending the Soap message failed or no 
      recognizable response was received 
    HRESULT=0x800A1527 - ClieConnector', wCode '0'

    Bump up the timeout. It might fix it.

  2. If WinINET connector to be used instead of WinHTTP, rename/delete whsc30.dll under program files\common files\MSSoap\Binaries.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVT_Dispatch to BSTR conversion issue Pin
Member 267324721-Aug-11 20:58
Member 267324721-Aug-11 20:58 
Questionhow to pass base64binary Pin
rsobies1-Oct-06 21:49
rsobies1-Oct-06 21:49 
QuestionSOAP Error Pin
SolomonGnanaraj5-Aug-06 2:04
SolomonGnanaraj5-Aug-06 2:04 
AnswerRe: SOAP Error Pin
jlclua25-Sep-06 14:06
jlclua25-Sep-06 14:06 
Questionerror in the client Pin
jamni19-Jul-06 17:31
jamni19-Jul-06 17:31 
QuestionRe: error in the client Pin
SolomonGnanaraj5-Aug-06 2:03
SolomonGnanaraj5-Aug-06 2:03 
Generalthis code example Pin
johncodep22-Jun-06 8:11
johncodep22-Jun-06 8:11 
QuestionUrgent Help:How to Use ProxyPort and Proxy Server Pin
mardan mustak12-Apr-06 1:49
mardan mustak12-Apr-06 1:49 
QuestionHi.. what about Dataset as a return value?? Pin
walrusman29-Nov-05 18:11
walrusman29-Nov-05 18:11 
Generalsolution for 0x800A1527 Pin
logicaldna8-Sep-05 21:36
logicaldna8-Sep-05 21:36 
GeneralRe: solution for 0x800A1527 Pin
amccool9-Sep-05 14:10
amccool9-Sep-05 14:10 
GeneralRe: solution for 0x800A1527 Pin
SrinivasV4-Jan-06 23:59
SrinivasV4-Jan-06 23:59 
GeneralRe: solution for 0x800A1527 Pin
dewins10-May-12 6:33
dewins10-May-12 6:33 
Generalsh SOAP Toolkit 3.0 Pin
mkharwadkar31-Jul-05 20:47
sussmkharwadkar31-Jul-05 20:47 
QuestionHow to get method names of the web service Pin
grinder13-Mar-05 21:49
grinder13-Mar-05 21:49 
GeneralSOAP C++ Pin
6-Mar-05 2:53
suss6-Mar-05 2:53 
GeneralRe: SOAP C++ Pin
z11aalex17-Dec-08 5:02
z11aalex17-Dec-08 5:02 
QuestionHELP ME!!!why toolkit3.0 generate these bad XML for me? Pin
gxulg16-Jan-05 19:05
gxulg16-Jan-05 19:05 
Hi, i have installed MS SOAP Toolkit3.0, the platform is WINDOWSXP+SP2, the code i wirte is listed below:
#include <stdio.h>
#import "msxml4.dll"
using namespace MSXML2;
#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")

using namespace MSSOAPLib30;
void Get()
{
HRESULT hr;

ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service.
hr = Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = L"http://update.waytech.com.cn/Webservices/WTAU/WTAU.asmx?WSDL";
hr = Connector->Connect();

// Begin the message.
Connector->Property["SoapAction"] = L"http://www.waytech.com.cn/webservices/WTAU/WTAU0";
hr = Connector->BeginMessage();

// Create the SoapSerializer object.
hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));

// Connect the serializer object to the input stream of the connector object.
hr = Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));

// Build the SOAP Message.

hr = Serializer->StartEnvelope("", "", "");
// Serializer->SoapNamespace(L"xsi", L"http://www.w3.org/2001/XMLSchema-instance");
// Serializer->SoapNamespace(L"xsd",L"http://www.w3.org/2001/XMLSchema");

hr = Serializer->StartBody(L"");

Serializer->StartElement(L"request", "", "", "");

hr = Serializer->StartElement(L"WTAU1", "", "","");
// Serializer->SoapNamespace("", "http://www.waytech.com.cn/webservices/WTAU");
hr = Serializer->StartElement(L"Module", "", "", "");

hr = Serializer->StartElement(L"ProdType","","","");
hr = Serializer->WriteString(L"003");
hr = Serializer->EndElement();

hr = Serializer->StartElement(L"ProdSubType","","","");
hr = Serializer->WriteString(L"003.0001");
hr = Serializer->EndElement();

hr = Serializer->StartElement(L"Version","","","");
hr = Serializer->WriteString(L"1.0.0.0");
Serializer->EndElement();

hr = Serializer->StartElement(L"TimeStamp","","","");
hr = Serializer->WriteString(L"2004 12 31 00:00:00");
hr = Serializer->EndElement();

hr = Serializer->StartElement(L"Name","","","");
hr = Serializer->WriteString(L"RV.EXE");
hr = Serializer->EndElement();

hr = Serializer->StartElement(L"FileName","","","");
hr = Serializer->WriteString(L"RV.EXE");
hr = Serializer->EndElement();

hr = Serializer->EndElement();
hr = Serializer->EndElement();
Serializer->EndElement();
hr = Serializer->EndBody();
hr = Serializer->EndEnvelope();


// Send the message to the XML Web service.
hr = Connector->EndMessage();

// Read the response.
hr = Reader.CreateInstance(__uuidof(SoapReader30));

// Connect the reader to the output stream of the connector object.
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");

// Display the result.
printf("Answer: %s\n", (const char*)Reader->Dom->xml);
}

int main()
{
CoInitialize(NULL);
Get();
CoUninitialize();
return 0;
}
I use a protocol sniffer to capture the request message it generate and found it is like these:
<?xml **UnReg**1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="" xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema"

xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/"

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="">
<request SOAP-ENV:encodingStyle="">
<WTAU1 SOAP-ENV:encodingStyle="">
<Module SOAP-ENV:encodingStyle="">
<ProdType SOAP-ENV:encodingStyle="">003</ProdType>
<ProdSubType SOAP-ENV:e**UnReg**yle="">003.0001</ProdSubType>
<Version SOAP-ENV:encodingStyle="">1.0.0.0</Version>
<TimeStamp SOAP-ENV:encodingStyle="">2004 12 31 00:00:00</TimeStamp>
<Name SOAP-ENV:encodingStyle="">RV.EXE</Name>
<FileName SOAP-ENV:encodingStyle="">RV.EXE</FileName>
</Module>
</WTAU1>
</request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

you can see that the first line is wrong, the node "ProdSubType" is wrong too, why will this happen, what should i do to sovle this problem, i hope you can help me.

learn to be still.
AnswerRe: HELP ME!!!why toolkit3.0 generate these bad XML for me? Pin
mehdi_tn12-Jul-05 22:31
mehdi_tn12-Jul-05 22:31 
QuestionHTTPS? Pin
Nirav Doshi23-Sep-04 7:13
Nirav Doshi23-Sep-04 7:13 
AnswerRe: HTTPS? Pin
nguyen Ngoc Tuan8-Jan-07 15:12
nguyen Ngoc Tuan8-Jan-07 15:12 
AnswerRe: HTTPS? Pin
Nirav Doshi8-Jan-07 21:04
Nirav Doshi8-Jan-07 21:04 
QuestionRe: HTTPS? Pin
SRANI9-Jun-08 5:00
SRANI9-Jun-08 5:00 
AnswerRe: HTTPS? Pin
Nirav Doshi9-Jun-08 5:44
Nirav Doshi9-Jun-08 5:44 
QuestionRe: HTTPS? Pin
SRANI9-Jun-08 10:16
SRANI9-Jun-08 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.