微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SoapClientBase ,A Good Class provide a mean to WebServices

#pragma once

 

/****************************************************************************************************

Soap client helper class for C++

Usage:

       1. Inherit a class from this base class

       2. Call Init in constructor,like this:

              Init("http://localhost/AuthService/AuthService.asmx?wsdl","AuthService","");

       3. Wrap the webservice by writing proxy function for each web method

       4. Call Invoke to execute the function,processing the input params and return value

              be careful,the order of parameters should be reversed in the parameter array while callin Invoke

       5. Then use this class to call webservice,like this:

              AuthServiceClient       service;

              bool ret = service.IsAuthorized(strCode);

 

*******************************************************************************************************/

 

//import soapsdk

//modify the path if needed

#import "C:/Program Files/Common Files/MSSoap/Binaries/mssoap30.dll" /

       exclude("IStream","IErrorInfo","ISequentialStream","_LARGE_INTEGER",/

       "_ULARGE_INTEGER","tagSTATSTG","_FILETIME")

using namespace MSSOAPLib30;

 

///////////////////////////////////////////////////////////////////////////////////////

// Base class for calling a webservice using soap

class SoapClientBase

{

protected:

       ISoapClient   *m_pSoapClient;

       char*             m_pError;

       HRESULT            m_hr;

 

public:

       SoapClientBase(void)

       {

              m_pSoapClient = NULL;

              m_pError = NULL;

              m_hr = 0;

       }

 

       virtual ~SoapClientBase(void)

       {

              Reset();

       }

 

       ///////////////////////////////////////////////////////////////////

       // Check error message issued by last call(if any)

       char* GetLastError()

       {

              return m_pError;

       }

 

       ////////////////////////////////////////////////////////////////

       // Init SoapClient object

       bool Init(char* szWSDLFile,char* szService,char* szPort)

       {

              Reset();

 

              //create soapclient object

              m_hr = ::CoCreateInstance(__uuidof(SoapClient30),NULL,CLSCTX_INPROC_SERVER,__uuidof(ISoapClient),(LPVOID *)&m_pSoapClient);

              if (m_pSoapClient==NULL)

              {

                     IssueError("Create soap client object fail");

                     return false;

              }

 

              //init soap client

              _variant_t     varWSDL      = szWSDLFile;

              _variant_t     varWSML = "";

              _bstr_t bstrService     = szService;

              _bstr_t bstrPort    = szPort;

              _bstr_t bstrNS             = "";

              m_hr = m_pSoapClient->MSSoapInit2(varWSDL,varWSML,bstrService,bstrPort,bstrNS);

              if (Failed(m_hr))

              {

                     IssueError("Error calling MSSoapInit2");

                     return false;

              }

 

              return true;

       }

 

 

protected:

       ///////////////////////////////////////////////////////////////////

       // Record an error message issued by this class or inherited

       void IssueError(char* szError)

       {

              if (m_pError)

                     delete [] m_pError;

 

              m_pError = new char[strlen(szError)+1];

              strcpy(m_pError,szError);

       }

 

       ////////////////////////////////////////////////////////////////////

       // Release interface,free memory,clean everything

       void Reset()

       {

              if (m_pSoapClient)

              {

                     m_pSoapClient->Release();

                     m_pSoapClient = NULL;

              }

              if (m_pError)

              {

                     delete [] m_pError;

                     m_pError = NULL;

              }

              m_hr = S_OK;

       }

 

protected:

       /////////////////////////////////////////////////////////////////////////////////////////

       // the following code is copied from ATL code(CComdispatchDriver),and modified

 

       HRESULT GetIDOfName(LPCOLESTR lpsz,disPID* pdispid)

       {

              return m_pSoapClient->GetIDsOfNames(IID_NULL,(LPOLESTR*)&lpsz,1,LOCALE_USER_DEFAULT,pdispid);

       }

       // Invoke a method by disPID with N parameters

       HRESULT Invoke(disPID dispid,VARIANT* pvarParams,int nParams,VARIANT* pvarRet = NULL)

       {

              disPParaMS dispparams = { pvarParams,nParams,0};

              return m_pSoapClient->Invoke(dispid,IID_NULL,disPATCH_METHOD,&dispparams,pvarRet,NULL);

       }

       // Invoke a method by name with Nparameters

       HRESULT Invoke(LPCOLESTR lpszName,VARIANT* pvarRet = NULL)

       {

              HRESULT hr;

              disPID dispid;

              hr = GetIDOfName(lpszName,&dispid);

              if (SUCCEEDED(hr))

                     hr = Invoke(dispid,pvarParams,pvarRet);

              return hr;

       }

};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐