C++如何动态调用Web Services

tomorrowzls 2014-01-22 03:50:47
求问C++如何动态调用Web Services? gSoap应该不行,因为它要根据wsdl手动生成头文件。现在的需求是可以支持扩展,对于之前没有的服务,知道其uri就可访问它的服务

ps:提供服务的接口名称已知
刚刚接触web services,可能描述不准确。
...全文
1090 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦幻与未来 2015-07-07
  • 打赏
  • 举报
回复
引用 1 楼 qiujialongjjj 的回复:
#include <stdio.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;  //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错


void Add()
{
   ISoapSerializerPtr Serializer;
   ISoapReaderPtr Reader;
   ISoapConnectorPtr Connector;
   // Connect to the service.
   Connector.CreateInstance(__uuidof(HttpConnector30));  //HttpConnector30 失败,无法这样创建Connector,CXX0017 Error :Symbol “HttpConnector30“ not found(摇头、叹气!)
   Connector->Property["EndPointURL"] = "http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl";  //这个当然得改成您自己的拉
   Connector->Connect();

   // Begin the message.
   //Connector->Property["SoapAction"] = "uri:AddNumbers";
   Connector->Property["SoapAction"] = "http://tempuri.org/DocSample1/action/Sample1.AddNumbers";
   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("");
   Serializer->StartElement("AddNumbers","http://tempuri.org/DocSample1/message/","","");  //这是本地的Web Services,实际中要指定命名空间
   Serializer->StartElement("NumberOne","","","");
   Serializer->WriteString("5");
   Serializer->EndElement();
   Serializer->StartElement("NumberTwo","","","");
   Serializer->WriteString("10");
   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);
    
}

int main()
{
   CoInitialize(NULL);
   Add();
   CoUninitialize();
   return 0;
}
更改 EndPointURL 属性的值. 在URL里指定你的服务器名. OK 总结一下必要的关键步骤 1.导入类型库 2.需要创建一个SoapConnector 3.下一步创建SoapSerializer 4.下一步把消息附加到SoapConnector的输入流 5.下一步读取结果.要读取服务器的回复,客户端应用需要使用SoapReader, 6.SoapReader被连接到SoapConnector输出流 7.用IXMLDOMElement对象可以从SoapReader里读到服务器的回复
我用你这种方法,为什么用C#写的web service那边收到的参数都是null呢
相偎 2015-04-15
  • 打赏
  • 举报
回复
编译的时候出错了,error C2872: 'tagDOMNodeType' : ambiguous symbol很多这样的错误...请问怎么解决,谢谢你...
cjzzmdn 2014-04-17
  • 打赏
  • 举报
回复
gsoap也可以吧?你写好gsoap头文件,用soapcpp2.exe生成客户端代码,也不用实现服务器方法
tomorrowzls 2014-01-26
  • 打赏
  • 举报
回复
引用 1 楼 qiujialongjjj 的回复:
#include <stdio.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;  //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错


void Add()
{
   ISoapSerializerPtr Serializer;
   ISoapReaderPtr Reader;
   ISoapConnectorPtr Connector;
   // Connect to the service.
   Connector.CreateInstance(__uuidof(HttpConnector30));  //HttpConnector30 失败,无法这样创建Connector,CXX0017 Error :Symbol “HttpConnector30“ not found(摇头、叹气!)
   Connector->Property["EndPointURL"] = "http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl";  //这个当然得改成您自己的拉
   Connector->Connect();

   // Begin the message.
   //Connector->Property["SoapAction"] = "uri:AddNumbers";
   Connector->Property["SoapAction"] = "http://tempuri.org/DocSample1/action/Sample1.AddNumbers";
   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("");
   Serializer->StartElement("AddNumbers","http://tempuri.org/DocSample1/message/","","");  //这是本地的Web Services,实际中要指定命名空间
   Serializer->StartElement("NumberOne","","","");
   Serializer->WriteString("5");
   Serializer->EndElement();
   Serializer->StartElement("NumberTwo","","","");
   Serializer->WriteString("10");
   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);
    
}

int main()
{
   CoInitialize(NULL);
   Add();
   CoUninitialize();
   return 0;
}
更改 EndPointURL 属性的值. 在URL里指定你的服务器名. OK 总结一下必要的关键步骤 1.导入类型库 2.需要创建一个SoapConnector 3.下一步创建SoapSerializer 4.下一步把消息附加到SoapConnector的输入流 5.下一步读取结果.要读取服务器的回复,客户端应用需要使用SoapReader, 6.SoapReader被连接到SoapConnector输出流 7.用IXMLDOMElement对象可以从SoapReader里读到服务器的回复
linux下能用吗?
风一样的大叔 2014-01-26
  • 打赏
  • 举报
回复
linux下能跑C++吗?
风一样的大叔 2014-01-23
  • 打赏
  • 举报
回复
#include <stdio.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;  //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错


void Add()
{
   ISoapSerializerPtr Serializer;
   ISoapReaderPtr Reader;
   ISoapConnectorPtr Connector;
   // Connect to the service.
   Connector.CreateInstance(__uuidof(HttpConnector30));  //HttpConnector30 失败,无法这样创建Connector,CXX0017 Error :Symbol “HttpConnector30“ not found(摇头、叹气!)
   Connector->Property["EndPointURL"] = "http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl";  //这个当然得改成您自己的拉
   Connector->Connect();

   // Begin the message.
   //Connector->Property["SoapAction"] = "uri:AddNumbers";
   Connector->Property["SoapAction"] = "http://tempuri.org/DocSample1/action/Sample1.AddNumbers";
   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("");
   Serializer->StartElement("AddNumbers","http://tempuri.org/DocSample1/message/","","");  //这是本地的Web Services,实际中要指定命名空间
   Serializer->StartElement("NumberOne","","","");
   Serializer->WriteString("5");
   Serializer->EndElement();
   Serializer->StartElement("NumberTwo","","","");
   Serializer->WriteString("10");
   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);
    
}

int main()
{
   CoInitialize(NULL);
   Add();
   CoUninitialize();
   return 0;
}
更改 EndPointURL 属性的值. 在URL里指定你的服务器名. OK 总结一下必要的关键步骤 1.导入类型库 2.需要创建一个SoapConnector 3.下一步创建SoapSerializer 4.下一步把消息附加到SoapConnector的输入流 5.下一步读取结果.要读取服务器的回复,客户端应用需要使用SoapReader, 6.SoapReader被连接到SoapConnector输出流 7.用IXMLDOMElement对象可以从SoapReader里读到服务器的回复

12,165

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧