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; ?????? } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- angularjs – Restangular – 嵌套资源
- 带有指令的AngularJS表单验证 – “myform.$valid”对我来说
- twitter-bootstrap – Laravel在模态窗口中打开一条路线
- Advanced Programming in UNIX Environment Episode 8
- Scala将单词保留为JSON字段名称,带有Json.writes [A](等效于
- Angular2(单击)动态加载组件视图
- axis,jax-ws,xfire客户端调用分析,以及webservice万能客户端
- 在Scala中将函数参数定义为“var”?
- 全局变量 – Vim全局变量和mapleader
- 如何开发angularjs拦截器来控制会话