原文中代码不全,下面是我的代码,webservice服务端为用Eclipse+java编写,客户端为vs2003,vc++控制台
#include <stdio.h>
#import "msxml4.dll"
using namespace MSXML2;
//---加入soap相应的动态库
#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
"_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30; //前提是安装SOAP Toolkit3.0
void Add()
{
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;
// Connect to the service.
Connector.CreateInstance(__uuidof(HttpConnector30)); //HttpConnector30 失败,无法这样创建
//红色为WEBSERVICE地址
Connector->Property["EndPointURL"] = "http://localhost:8081/WebServiceProject/services/CalculateService?wsdl";
Connector->Connect();
// Begin the message.
Connector->Property["SoapAction"] = "";//可以为空详细查看SoapAction属性特征
Connector->BeginMessage();
// Create the SoapSerializer object.
Serializer.CreateInstance(__uuidof(SoapSerializer30));
// Connect the serializer object to the input stream of the connector object.
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));
// Build the SOAP Message.
Serializer->StartEnvelope("","","");
Serializer->StartBody("");
//这是本地的Web Services,实际中要指定命名空间
Serializer->StartElement("plus","http://service.rong","","");//名字空间不能少
Serializer->StartElement("x","http://service.rong","","");//名字空间不能少
Serializer->WriteString("10");
Serializer->EndElement();
Serializer->StartElement("y","http://service.rong","",""); //名字空间不能少
Serializer->WriteString("20");
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
// Send the message to the XML Web service.
Connector->EndMessage();
// Read the response.
Reader.CreateInstance(__uuidof(SoapReader30));
// Connect the reader to the output stream of the connector object.
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");
// Display the result.
printf("Answer: %s\n", (const char*)Reader->RpcResult->text);
printf("Answer: %s\n", (const char*)Reader->Body->xml);
}
int main(int argc, char* argv[])
{
CoInitialize(NULL);//初始化COM
Add();
CoUninitialize(); //与前面相对应
return 0;
}
总结一下必要的关键步骤
1.导入类型库
2.需要创建一个SoapConnector
3.下一步创建SoapSerializer
4.下一步把消息附加到SoapConnector的输入流
5.下一步读取结果.要读取服务器的回复,客户端应用需要使用SoapReader,
6.SoapReader被连接到SoapConnector输出流
7.用IXMLDOMElement对象可以从SoapReader里读到服务器的回复
ps:1.ms和开源的gsoap都是转成文字流,使用soap协议(或许本质就是http协议,或许本质就是socket网络编程(使用80端口))
2.soap由于采用了xml作为内容,其跨平台和可读性比http强很多,就像一个图片文件如果用记事本打开是一堆乱码,用图片查看器就是漂亮的画面;一组数据用fopen打开是字节,用oracle+sql打开就是数据库,很轻松统计,更新;用excel还可以填充颜色...
转自:http://blog.sina.com.cn/s/blog_4b34c6790100ub6q.html
参考:http://dsz211.blog.163.com/blog/static/9097859120088221911188/