18,356
社区成员
发帖
与我相关
我的任务
分享
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// 与Web服务连接
Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = "http://127.0.0.1:9080/helloWorld?wsdl";
Connector->Connect();
//指定webservice的网络资源路径
// 开始消息
Connector->Property["SoapAction"] = "http://demo.com/sayHi"; //_wsNameSapce + _wsMethodName _wsNameSapce参考?wsdl的结果
Connector->BeginMessage();
// 创建SoapSerializer对象
Serializer.CreateInstance(__uuidof(SoapSerializer30));
// 将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// 创建SOAP消息
Serializer->StartEnvelope("","","");
Serializer->StartBody(L"");
//构建一个soap协议报文
Serializer->StartElement("sayHi","http://demo.com/","","");
string strName = "testtest";
Serializer->StartElement( L"text" , "" , "" , "" ) ;
Serializer->SoapAttribute("text", "", "xs:string", "");
Serializer->WriteString(strName.c_str());
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
try
{
// 将该消息发送给web服务
Connector->EndMessage();
}
catch( CException * ce )
{
ce->ReportError( ) ;
ce->Delete( ) ;
return ;
}
catch( ...)
{
return ;
}
//通过http协议发送请求和接受响应
// 读取响应
Reader.CreateInstance(__uuidof(SoapReader30));
// 将reader联接到connector的输出字符串
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
// 显示结果
CString strMsg ;
IXMLDOMDocumentPtr domeDocument = Reader->GetDom( ) ;
strMsg.Format( "%s" , (const char*)domeDocument->xml ) ;
strMsg.Replace( "<?xml version=\"1.0\"?>" , "<?xml version=\"1.0\" encoding=\"GB2312\"?>" ) ;
AfxMessageBox( strMsg ) ;
用gSoap的方式要更方便Serializer->StartElement("getWeather", "http://WebXml.com.cn/", "", "Soap");
最后一个参数是名字空间,相应的SOAP请求是这样的:
POST /WebServices/WeatherWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getWeather"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getWeather xmlns="http://WebXml.com.cn/">
<theCityCode>string</theCityCode>
<theUserID>string</theUserID>
</getWeather>
</soap:Body>
</soap:Envelope>