声明了一个结构
typedef struct _mysoaparm
{
TCHAR parm_name[20];
TCHAR parm_value[8000];
}MYSOAPARM,*PMYSOAPARM;
/* 通用WebService查询接口 */
BOOL CMySoap::query(TCHAR* EndPointURL,//WebService URL
TCHAR* NameSpace, //命名空间
TCHAR* Method,//方法
int argv_len,//参数个数
MYSOAPARM* pargs,//参数
TCHAR* pRes//返回值
)
{
CString szAction;
HRESULT hr;
int errcode = 0;
try
{
Connector->Property["EndPointURL"] = (LPSTR)(LPCTSTR)EndPointURL;
hr = Connector->Connect(); //连接服务器
if FAILED(hr) throw errcode;
szAction.Format(_T("%s%s"),NameSpace,Method);
Connector->Property["SoapAction"] = (LPSTR)(LPCTSTR)szAction;
Connector->BeginMessage();
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// 创建SOAP消息
Serializer->StartEnvelope("","","");
Serializer->StartBody("");
Serializer->StartElement(Method,(LPSTR)(LPCTSTR)NameSpace,"","");
for(int i=0;i<argv_len;i++)
{
Serializer->StartElement(pargs[i].parm_name,"","","");//命名空间必须有
Serializer->WriteString(pargs[i].parm_value);
Serializer->EndElement();
}
//结束SOAP消息创建
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
// Send the message to the web service
Connector->EndMessage();
//读取返回值
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
strcpy(pRes,(const char*)Reader->RpcResult->text);
}
catch(...)
{
return FALSE;
}
return TRUE;
}
下面是一个调用的例子
/* 发送短信接口 */
int CMySoap::smsSendMessage(CString szMobile,
CString szContent,
CString SEQ,
CString szSendTime,
CString szTask)
{
int ErrCode = 0;
TCHAR szResponse[10] = {0};
MYSOAPARM accParm[9]; //smsSend
if(szMobile.GetLength()<=0) return -100;
if(szContent.GetLength()<=0) return -100;
//第一个参数
strcpy(accParm[0].parm_name,_T("ECODE"));
strcpy(accParm[0].parm_value,this->ECode);
//第二个参数
strcpy(accParm[1].parm_name,_T("EXTNO"));
strcpy(accParm[1].parm_value,this->ExtNo);
//第三个参数
strcpy(accParm[2].parm_name,_T("USERNAME"));
strcpy(accParm[2].parm_value,this->UserName);
//第四个参数
strcpy(accParm[3].parm_name,_T("PASSWORD"));
strcpy(accParm[3].parm_value,this->Password);
//第五个参数
strcpy(accParm[4].parm_name,_T("MOBILE"));
strcpy(accParm[4].parm_value,(LPSTR)(LPCTSTR)szMobile);
//第六个参数
strcpy(accParm[5].parm_name,_T("CONTENT"));
strcpy(accParm[5].parm_value,(LPSTR)(LPCTSTR)szContent);
//第七个参数
strcpy(accParm[6].parm_name,_T("SEQ"));
strcpy(accParm[6].parm_value,(LPSTR)(LPCTSTR)SEQ);
//第八个参数
strcpy(accParm[7].parm_name,_T("SENDTIME"));
strcpy(accParm[7].parm_value,(LPSTR)(LPCTSTR)szSendTime);
//第九个参数
strcpy(accParm[8].parm_name,_T("TASK"));
strcpy(accParm[8].parm_value,(LPSTR)(LPCTSTR)szTask);
//调用通用查询并获得结果
if(!this->query((LPSTR)(LPCTSTR)this->szURL,
(LPSTR)(LPCTSTR)this->szNameSpace,
_T("smsSend"),
9,
accParm,
szResponse))
return FALSE;
ErrCode = atoi(szResponse);
return ErrCode;
}
调用之前需要对SOAP对象做初始化
/* 初始化SOAP对象 */
BOOL CMySoap::OfInit()
{
// Connect to the service
HRESULT hr;
hr = Connector.CreateInstance(__uuidof(HttpConnector30));
if(FAILED(hr)) return FALSE;
hr = Serializer.CreateInstance(__uuidof(SoapSerializer30));
if(FAILED(hr)) return FALSE;
hr = Reader.CreateInstance(__uuidof(SoapReader30));
if(FAILED(hr)) return FALSE;
return TRUE;
}