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, NULL, nParams, 0};

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

       }

       // Invoke a method by name with Nparameters

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

       {

              HRESULT hr;

              DISPID dispid;

              hr = GetIDOfName(lpszName, &dispid);

              if (SUCCEEDED(hr))

                     hr = Invoke(dispid, pvarParams, nParams, pvarRet);

              return hr;

       }

};

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值