gsoap 相关使用(wsdl2h 和 soapcpp2)

gsoap 相关介绍

介绍地址:http://blog.csdn.net/darkone/article/details/1442525

gsoap 下载

下载地址:https://www.cs.fsu.edu/~engelen/soap.html

gsoap 代码生成

1. wsdl2h 根据 wsdl 和 xml 等文件生成 *.h 文件 (此文件是一个中间文件,在不程序实现中没有作用-本人看法)

2. soapcpp2 根据 *.h 文件生成 对应的service和client程序代码(生成文件固定包含”soapH.h,soapStub.h,soapC.cpp” 3个文件 和 多组”*Proxy.h,*Proxy.cpp,*.nsmap” client 文件 和 多组”*Service.h,*Service.cpp,*.nsmap” service 文件)

3. 示例:(更具gsoap提供的计数器wdsl编写下面命令)

@echo 下面代码为window(bat)脚本
 set path=calc
 set serverPath=calcService 
 mkdir %path%
 @echo 根据 calc.wsdl 生成 calc.h 文件
 @echo《wsdl文件可以下载到本地使用》
 @echo 例子: wsdl2h  -s -o calc.h -f -p -n calc %文件路径%\calc.wsdl
 wsdl2h  -s -o calc.h -f -p -n calc http://www.cs.fsu.edu/~engelen/calc.wsdl
 @echo 根据 calc.h 文件生成c++ client客户端代码
 soapcpp2 -d %path% calc.h -C -x -i -v -c
 @echo 根据 calc.h 文件生成 c++ server服务端代码
 soapcpp2 -d %serverPath% calc.h -S -x -i -v -c

注意:本人是在windows下使用的gsoap工具(使用gsoap编译好的程序)。目前gsoap官方编译好的程序无法对 https 公布的wsdl等文件进行在线生成*.h文件(错误提示:Cannot connect to https site: SSL/TLS support not enabled, please rebuild wsdl2h with SSL/TLS enabled using ‘make secure’ or download the WSDL/WADL and XSD files and rerun wsdl2h on these files directly by specifying the file names on the command line.),所以需要下载wsdl等文件或者重新对wsdl2h进行使用openssl编译(本人在windows下没有编译成功,同时也查找了很多资料也没有找到对应的编译方法,希望有在window编译成功带openssl信息的gsoap大牛们可以分享一下) 本人使用的gsoap版本《2.8.55》

4. wsdl2h 和 soapcpp2命令参数

介绍地址:http://blog.csdn.net/wskqw/article/details/78753682

gsoap 代码的使用

1. client 调用过程

  • 将生产的代码考入到工程目录下(soapH.h,soapStub.h,soapC.cpp,Proxy.cpp,*Proxy.h,.nsmap 以及gsoap目录下的stdsoap2.cpp,stdsoap2.h 本人生成的C++客户端代码所有需要这两个文件)
  • 调用过程
    #include “calc-gsoap/calc.nsmap”
    #include “calc-gsoap/soapcalcProxy.h”
    int main()
    {
    calcProxy calcPro(“http://websrv.cs.fsu.edu/~engelen/calcserver.cgi“);
    double dst = 0;
    calcPro.add(3,4,dst);
    std::cout<< “calcProxy.add(3,4) = ” << dst << std::endl;
    system(“pause”);
    }
    《注意》:编译错误提示:
    1. stdsoap2.obj : error LNK2001: 无法解析的外部符号 _namespaces
    原因:没有引用“calc.nsmap”文件
    2. soapC.obj : error LNK2019: 无法解析的外部符号 _soap_match_tag,该符号在函数 _soap_getelement 中被引用
    原因:没有添加“stdsoap2.cpp”到工程中文件

2. service的实现过程(gsoap官网的事例代码)

#include “soapcalcService.h”
#include “calc.nsmap”

int main(int argc, char **argv)
{
calcService calc;
if (argc < 2)
calc.serve(); /* serve as CGI application */
else
{ int port = atoi(argv[1]);
if (!port)
{ fprintf(stderr, “Usage: calcserver++ \n”);
exit(0);
}
/* run iterative server on port until fatal error */
if (calc.run(port))
{ calc.soap_stream_fault(std::cerr);
exit(-1);
}
}
return 0;
}

int calcService::add(double a, double b, double *result)
{ *result = a + b;
return SOAP_OK;
}

int calcService::sub(double a, double b, double *result)
{ *result = a - b;
return SOAP_OK;
}

int calcService::mul(double a, double b, double *result)
{ result = a b;
return SOAP_OK;
}

int calcService::div(double a, double b, double *result)
{ if (b)
*result = a / b;
else
{ char s = (char)soap_malloc(this, 1024);
(SOAP_SNPRINTF(s, 1024, 100), “http://tempuri.org/\”>Can’t divide %f by %f“, a, b);
return soap_senderfault(“Division by zero”, s);
}
return SOAP_OK;
}

int calcService::pow(double a, double b, double *result)
{ *result = ::pow(a, b);
if (soap_errno == EDOM) /* soap_errno is like errno, but compatible with Win32 */
{ char s = (char)soap_malloc(this, 1024);
(SOAP_SNPRINTF(s, 1024, 100), “http://tempuri.org/\”>Can’t take power of %f to %f“, a, b);
return soap_senderfault(“Power function domain error”, s);
}
return SOAP_OK;
}

《注意》:在gsoap的源码中包含很多事例。事例在文件“gsoap\samples”下

猜你喜欢

转载自blog.csdn.net/wskqw/article/details/78754400