vc Microsoft SOAP Toolkit 3.0 代码(转)

25 篇文章 0 订阅
9 篇文章 0 订阅

原文中代码不全,下面是我的代码,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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值