首先应该安装soap toolkit3.0
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=13456
运行环境为windows xp sp3,无需配置其它任何环境,直接建立一个控制台应用程序运行以下代码即可。
#include <stdio.h>
//#include "windows.h"
#import "msxml4.dll"
using namespace MSXML2;
#import "C:/Program Files/Common Files/MSSoap/Binaries/mssoap30.dll" exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER","_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30;
void Add()
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service.
if(FAILED(Connector.CreateInstance(__uuidof(HttpConnector30)))) //创建对象
{
printf("失败");
}
Connector->Property["EndPointURL"] ="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"; //wsdl路径
Connector->Connect();
// Begin the message. //消息体
Connector->Property["SoapAction"] = "http://WebXml.com.cn/getWeather"; //函数体参数
Connector->BeginMessage();
Serializer.CreateInstance(__uuidof(SoapSerializer30));
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// Build the SOAP Message.
Serializer->StartEnvelope("Soap","","");
Serializer->StartBody("");
Serializer->StartElement("getWeather","http://WebXml.com.cn/","","Soap"); //函数处理
Serializer->StartElement("theCityCode","","","Soap");
Serializer->WriteString("郑州"); //参数处理
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
Connector->EndMessage();
Reader.CreateInstance(__uuidof(SoapReader30));
printf("here");
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream),""); //加载返回数据
// Display the result.
//printf("Answer:%s\n", (const char*)Reader->Body->xml);
printf("Answer:%s\n",(const char*)Reader->RpcResult->text);
}
int main()
{
CoInitialize(NULL);
Add();
CoUninitialize();
return 0;
}
附注:1、当无输入参数时,需要删除以下三条语句:
Serializer->StartElement("theCityCode","","","Soap");
Serializer->WriteString("南京"); //参数处理
Serializer->EndElement();
2、当webservice为java所写时:
(1)Connector->Property["EndPointURL"]为函数所在页面的地址,如:
http://192.168.1.9:7001/leohis/services/GetTime
(2)Connector->Property["SoapAction"]可以同上,也可以去掉”/GetTime”
(3)Serializer->StartElement("getWeather","http://WebXml.com.cn/","","Soap")中的第二个参数为wsdl界面文件中的”targetNamespace”
3、当webservice为asp.net所写,则按照上面的程序来写。