gsoap常规用法:
- 通过wsdl文件创建头文件
//通过wsdl文件创建头文件
wsdl2h [options] -o file.h ... WSDL and XSD files or URLs to WSDLs and XSDs ...
- 通过头文件创建cpp实现源文件
//通过头文件创建cpp实现源文件
soapcpp2 [options] file.h
- 将生成的下列文件拷贝到项目中使用
soapClient.c[pp] client-side stub functions
soapServer.c[pp] server-side dispatch functions
soapC.c[pp] XML serializers for all XML SOAP API function arguments
soapH.h XML serializers and service API functions
soapStub.h cleaned-up copy of the file.h declarations, this file is included by soapH.h
prefix.nsmap XML namespace bindings table to #include in your code
4.不过一般情况通过soapcpp2参数-j控制创建c++更强大的代理类和服务类
soapNameProxy.h
soapNameProxy.cpp
soapNameService.h
soapNameService.cpp
5.拷贝文件到项目使用
6.服务端用法
#include "soapName.nsmap" // XML namespace mapping table (only needed once at the global level)
#include "soapNameService.h" // the service class, also #includes "soapH.h" and "soapStub.h"
int main()
{
soapNameService srv(SOAP_XML_INDENT);
if (srv.serve() != SOAP_OK)
srv.soap_stream_fault(std::cerr);
srv.destroy(); // same as: soap_destroy(srv.soap); soap_end(srv.soap);
}
int srv::add(double a, double b, double &result)
{
result = a + b;
return SOAP_OK;
}
6.客户端用法
#include "soapName.nsmap" // XML namespace mapping table (only needed once at the global level)
#include "soapNameProxy.h" // the proxy class, also #includes "soapH.h" and "soapStub.h"
int main()
{
soapNameProxy cli;
double sum;
if (cli.add(1.23, 4.56, sum) == SOAP_OK)
std::cout << "Sum = " << sum << std::endl;
else
cli.soap_stream_fault(std::cerr);
cli.destroy(); // same as: soap_destroy(cli.soap); soap_end(cli.soap);
}
多wsdl集成
但是很多时候,一个项目中可能即要做服务端,又要连接其他服务,做客户端,或者同时需要连接多个服务端,调用接口,又或者多个服务端,如果仅仅使用常规方法,多调用几次,soapC.c[pp],soapH.h,soapStub.h,prefix.nsmap文件会被覆盖,或者提前拷贝走,放入一个项目,会存在很多基础方法实现重复定义等问题,需要特殊处理.
- 通过多个wsdl文件创建多个头文件
//通过wsdl文件创建头文件1
wsdl2h.exe -o Service1.h -t typemap.dat service1.wsdl
//通过wsdl文件创建头文件2
wsdl2h.exe -o Service2.h -t typemap.dat service2.wsdl
- 通过头文件创建cpp实现源文件
//通过头文件创建cpp实现源文件1
soapcpp2.exe -C -L -j -x -I import Service1.h
//通过头文件创建cpp实现源文件2
soapcpp2.exe -C -L -j -x -I import Service2.h
- 创建单个统一的头文件
//通过wsdl文件1,2创建单个统一头文件
wsdl2h.exe -o ServiceAll.h -t typemap.dat service1.wsdl service2.wsdl
- 创建单个统一的实现源文件
//通过wsdl文件1,2创建cpp实现源文件2
soapcpp2.exe -C -L -j -x -I import ServiceAll.h
5.删掉多余的重复.nsmap文件,留一个就可以了
6.拷贝到项目中使用即可,同单wsdl模式.