C++调用WebService

  1. 1. gSOAP是一个开源的项目,用它可以方便的使用c/c++地进行SOAP客户端和服务器端编程,而不必了解xml和SOAP协议的细节
  2. wsdl2h.exe: 编译wsdl文件生成c/c++头文件
  3. -o 文件名,指定输出头文件
  4. -n 名空间前缀 代替默认的ns
  5. -c 产生纯C代码,否则是C++代码
  6. -s 不要使用STL代码
  7. -t 文件名,指定type map文件,默认为typemap.dat
  8. -e 禁止为enum成员加上名空间前缀
  9. soapcpp2.exe: gSOAP编译器,编译头文件生成服务器和客户端都需要的c/c++文件(默认使用STL,需要stlvector.h)
  10. -C 仅生成客户端代码
  11. -S 仅生成服务器端代码
  12. -L 不要产生soapClientLib.c和soapServerLib.c文件
  13. -c 产生纯C代码,否则是C++代码(与头文件有关)
  14. -I 指定import路径(见上文)
  15. -x 不要产生XML示例文件
  16. -i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)
  17. 2. 生成存客户端存根程序和框架
  18. wsdl2h -o xxx.h xxx.wsdl(-t D:/gsoap-2.7/gsoap/typemap.dat)
  19. soapcpp2 -L -x -i xxx.h(-I D:/gsoap-2.7/gsoap/import)
  20. 3. 或直接自己编写xxx.h,并生成xxx.wsdl
  21. //gsoap ns service name: xxx
  22. //gsoap ns service style: rpc
  23. //gsoap ns service encoding: literal
  24. //gsoap ns service location: http://localhost:8080
  25. //gsoap ns schema namespace: http://localhost:8080/xxx.wsdl
  26. int ns__add(int a, int b, int* result);
  27. 3. 客户端
  28. 1) 把如下生成的文件添加到项目:
  29. stdsoap2.h,stdsoap2.cpp,
  30. soapH.h,soapC.cpp,soapStub.h,
  31. soapxxxProxy.h,soapxxxProxy.cpp,xxxSoap.nsmap
  32. wsock32.lib
  33. 2) 代理方式调用
  34. #include "soapTestWebServiceProxy.h"
  35. #include "TestWebService.nsmap"
  36. int main(int argc, char* argv[])
  37. {
  38. int result = 0;
  39. TestWebServiceProxy proxy;
  40. proxy.add(12, 23, &result);
  41. }
  42. 4. 服务端
  43. 1) 把如下生成的文件添加到项目:
  44. stdsoap2.h,stdsoap2.cpp,
  45. soapH.h,soapC.cpp,soapStub.h,
  46. soapxxxService.h,soapxxxService.cpp,xxxSoap.nsmap
  47. wsock32.lib
  48. 2) 实现接口函数
  49. int TestWebServiceService::add(int a, int b, int *result)
  50. {
  51. *result = a+b;
  52. return SOAP_OK;
  53. }
  54. 3) 开启服务
  55. #include "soapTestWebServiceService.h"
  56. #include "TestWebService.nsmap"
  57. int main(int argc, char* argv[])
  58. {
  59. TestWebServiceService service;
  60. service.run(8080);
  61. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值