VC调用Webservice(本文为原创,转载清注明出外)
用C++调用axis2的WebService,用Sproxy.exe生成C++代码总是不成功.只好采用MS原始的COM接口.
1.首先初始化连接:
Code
// 初始化连接
CComQIPtr<ISoapConnector> CWebServices::CreateHttpConnector(CComBSTR webUrl)
{
HRESULT hr;
CComQIPtr<ISoapConnector> connector;
connector.CoCreateInstance(__uuidof(HttpConnector30));
if (connector){
connector->Property["EndPointURL"] = CComVariant(webUrl);
hr = connector->Connect();
if (SUCCEEDED(hr)){
connector->Property["SoapAction"] = CComVariant("http://localhost/");
return connector;
}
}
else {
CUtils::WriteLog(true,_T("您的机器没有安装SoapToolkit30,请下载安装!"));
}
return CComQIPtr<ISoapConnector>();
}
// 初始化连接
CComQIPtr<ISoapConnector> CWebServices::CreateHttpConnector(CComBSTR webUrl)
{
HRESULT hr;
CComQIPtr<ISoapConnector> connector;
connector.CoCreateInstance(__uuidof(HttpConnector30));
if (connector){
connector->Property["EndPointURL"] = CComVariant(webUrl);
hr = connector->Connect();
if (SUCCEEDED(hr)){
connector->Property["SoapAction"] = CComVariant("http://localhost/");
return connector;
}
}
else {
CUtils::WriteLog(true,_T("您的机器没有安装SoapToolkit30,请下载安装!"));
}
return CComQIPtr<ISoapConnector>();
}
2.发送SOAP
Code
************************************************************************/
/* 得到Soap序列化 */
/************************************************************************/
CComQIPtr<ISoapSerializer> CWebServices::GetSoapSerializer(CComQIPtr<ISoapConnector> connector)
{
CComQIPtr<ISoapSerializer> serializer;
if (connector) {
if (SUCCEEDED(connector->BeginMessage()))
{
serializer.CoCreateInstance(__uuidof(SoapSerializer30));
if (serializer){
serializer->Init(_variant_t((IUnknown*)connector->InputStream));
return serializer;
}
}
}
return CComQIPtr<ISoapSerializer>();
}
************************************************************************/
/* 得到Soap序列化 */
/************************************************************************/
CComQIPtr<ISoapSerializer> CWebServices::GetSoapSerializer(CComQIPtr<ISoapConnector> connector)
{
CComQIPtr<ISoapSerializer> serializer;
if (connector) {
if (SUCCEEDED(connector->BeginMessage()))
{
serializer.CoCreateInstance(__uuidof(SoapSerializer30));
if (serializer){
serializer->Init(_variant_t((IUnknown*)connector->InputStream));
return serializer;
}
}
}
return CComQIPtr<ISoapSerializer>();
}
3. 读取response
Code
//返回读取器
CComQIPtr<ISoapReader> CWebServices::GetSoapReader(CComQIPtr<ISoapConnector> connector)
{
CComQIPtr<ISoapReader> reader;
if (SUCCEEDED(reader.CoCreateInstance(__uuidof(SoapReader30))))
{
VARIANT_BOOL _hr;
_hr = reader->Load(_variant_t((IUnknown*)connector->OutputStream), _bstr_t(""));
if (_hr == VARIANT_TRUE)
return reader;
}
return CComQIPtr<ISoapReader>();
}
//返回读取器
CComQIPtr<ISoapReader> CWebServices::GetSoapReader(CComQIPtr<ISoapConnector> connector)
{
CComQIPtr<ISoapReader> reader;
if (SUCCEEDED(reader.CoCreateInstance(__uuidof(SoapReader30))))
{
VARIANT_BOOL _hr;
_hr = reader->Load(_variant_t((IUnknown*)connector->OutputStream), _bstr_t(""));
if (_hr == VARIANT_TRUE)
return reader;
}
return CComQIPtr<ISoapReader>();
}
调用过程只要通过ISoapSerializer接口把SOAP消息拼装发送就可以了.
(假如调用setCenEAIBatchMessage方法,有一个XML参数)
Code
CComQIPtr<ISoapConnector> pConnector = CreateHttpConnector(m_url);
CComQIPtr<ISoapSerializer> pSerializer = GetSoapSerializer(pConnector);
if (pSerializer){
pSerializer->StartEnvelope("SOAP","","");
pSerializer->StartBody("");
pSerializer->StartElement("setCenEAIBatchMessage",_bstr_t(m_nameSpace),"","");
pSerializer->StartElement("xml",_bstr_t(m_nameSpace),"","");
pSerializer->WriteString(_bstr_t(paramter));
pSerializer->EndElement();
pSerializer->EndElement();
pSerializer->EndBody();
pSerializer->EndEnvelope();
pConnector->EndMessage();
}
CComQIPtr<ISoapConnector> pConnector = CreateHttpConnector(m_url);
CComQIPtr<ISoapSerializer> pSerializer = GetSoapSerializer(pConnector);
if (pSerializer){
pSerializer->StartEnvelope("SOAP","","");
pSerializer->StartBody("");
pSerializer->StartElement("setCenEAIBatchMessage",_bstr_t(m_nameSpace),"","");
pSerializer->StartElement("xml",_bstr_t(m_nameSpace),"","");
pSerializer->WriteString(_bstr_t(paramter));
pSerializer->EndElement();
pSerializer->EndElement();
pSerializer->EndBody();
pSerializer->EndEnvelope();
pConnector->EndMessage();
}