gsoap+URL自动生成头文件与客户端代码流程

1、下载gsoap代码下载链接 https://download.csdn.net/download/u011186256/10711057

    下载以后解压,进入\gsoap-2.8\gsoap-2.8\gsoap\bin\win32这个目录下面有两个执行文件soapcpp2.exe 与wsdl2h.exe

    wsdl2h.exe这个是生成头文件的

    soapcpp2.exe这个是生成代码的。

2、以天气预报的url为例生成头文件

    天气预报的URL:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

3、cmd命令进入wsdl2h.exe这个所在的目录然后运行

命令解释:
wsdl2h -o 头文件名 WSDL文件名或URL
wsdl2h常用选项
-o 文件名,指定输出头文件
-n 名空间前缀 代替默认的ns
-c 产生纯C代码,否则是C++代码
-s 不要使用STL代码
-t 文件名,指定type map文件,默认为typemap.dat
-e 禁止为enum成员加上名空间前缀

  运行命令:.\wsdl2h -o test.h http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

生成test.h头文件,正常运行打印如下:

gsoap-2.8\gsoap-2.8\gsoap\bin\win32>.\wsdl2h -o test.h http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

**  The gSOAP WSDL/Schema processor for C and C++, wsdl2h release 2.8.8
**  Copyright (C) 2000-2012 Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The wsdl2h tool is released under one of the following two licenses:
**  GPL or the commercial license by Genivia Inc. Use option -l for details.

Saving test.h

Cannot open file 'typemap.dat'
Problem reading type map file 'typemap.dat'.
Using internal type definitions for C++ instead.


Connecting to 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl' to retrieve WSDL/XSD...
Connected, receiving...
Done reading 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl'

To complete the process, compile with:
> soapcpp2 test.h
or to generate C++ proxy and object classes:
> soapcpp2 -j test.h

根据提示运行soapcpp2 test.h或者soapcpp2 -j test.h会有错误提示如下:

**  The gSOAP code generator for C and C++, soapcpp2 release 2.8.8
**  Copyright (C) 2000-2012, Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The soapcpp2 tool is released under one of the following two licenses:
**  GPL or the commercial license by Genivia Inc.

Critical error: #import: Cannot open file "stlvector.h" for reading.
Hint: use option -I (you can define multiple paths separated with ';')

解决这个问题要将gsoap-2.8\gsoap\import下的soap12.h,stdsoap2.h,stlvector.h,与./gsoap/stdsoap2.cpp下的stdsoap.cpp这几个文件复制到wsdl2h.exe所在的目录下。

然后运行soapcpp2 -j test.h或者soapcpp2 test.h,运行以后正确打印结果如下:

**  The gSOAP code generator for C and C++, soapcpp2 release 2.8.8
**  Copyright (C) 2000-2012, Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The soapcpp2 tool is released under one of the following two licenses:
**  GPL or the commercial license by Genivia Inc.

Saving soapStub.h annotated copy of the input declarations
Using ns2 service name: WeatherWebServiceSoap
Using ns2 service style: document
Using ns2 service encoding: literal
Using ns2 service location: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
Using ns2 schema namespace: http://WebXml.com.cn/WeatherWebServiceSoap
Saving soapWeatherWebServiceSoapProxy.h client proxy
Saving soapWeatherWebServiceSoapObject.h server object
Saving WeatherWebServiceSoap.getSupportCity.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap.getSupportCity.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap.getSupportProvince.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap.getSupportProvince.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap.getSupportDataSet.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap.getSupportDataSet.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap.getWeatherbyCityName.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap.getWeatherbyCityName.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap.getWeatherbyCityNamePro.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap.getWeatherbyCityNamePro.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap.nsmap namespace mapping table
Using ns3 service name: WeatherWebServiceSoap12
Using ns3 service style: document
Using ns3 service encoding: literal
Using ns3 service location: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
Using ns3 schema namespace: http://WebXml.com.cn/WeatherWebServiceSoap12
Saving soapWeatherWebServiceSoap12Proxy.h client proxy
Saving soapWeatherWebServiceSoap12Object.h server object
Saving WeatherWebServiceSoap12.getSupportCity.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap12.getSupportCity.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap12.getSupportProvince.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap12.getSupportProvince.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap12.getSupportDataSet.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap12.getSupportDataSet.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap12.getWeatherbyCityName.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap12.getWeatherbyCityName.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap12.getWeatherbyCityNamePro.req.xml sample SOAP/XML request
Saving WeatherWebServiceSoap12.getWeatherbyCityNamePro.res.xml sample SOAP/XML response
Saving WeatherWebServiceSoap12.nsmap namespace mapping table
Saving soapClient.cpp client calling stubs
Saving soapClientLib.cpp client stubs with serializers (use only for libs)
Saving soapServer.cpp server request dispatcher
Saving soapServerLib.cpp server request dispatcher with serializers (use only for libs)
Saving soapH.h interface declarations
Saving soapC.cpp XML serializers

Compilation successful

然后运行

soapcpp2 -i -C -x alldevices.h

soapcpp2常用选项:

-C 仅生成客户端代码 

-S 仅生成服务器端代码 

-L 不要产生soapClientLib.c和soapServerLib.c文件 

-c 产生纯C代码,否则是C++代码(与头文件有关) 

-I 指定import路径(视自己的解压包目录而定);

-x 不要产生XML示例文件 

-i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)

生成相关的代码。

4、导入vs2015工程中,创建工程,去掉预编译头,与工程采用Unicode字符集如下面两个图。

gsoap+URL自动生成头文件与客户端代码流程_第1张图片

gsoap+URL自动生成头文件与客户端代码流程_第2张图片

然后导入项目,在VC里新建项目,找到项目所在文件夹,将生成的soapC.cpp,soapH.h,soapStub.h,XXXWebServiceSoapProxy.cpp,XXXWebServiceSoapProxy.h,XXX.nsmap以及gSOAP系统文件stdsoap2.cpp和stdsoap2.h文件复制到该项目文件夹,同时加载到工程中。

其中soapC.cpp,soapH.h,soapStub.h,XXXWebServiceSoapProxy.cpp,XXXWebServiceSoapProxy.h,XXX.nsmap这几个文件在soapcpp2.exe所在目录下。stdsoap2.cpp和stdsoap2.h这两个文件在gsoap-2.8\gsoap这个目录下。

 

参考链接:https://blog.csdn.net/u013834105/article/details/76421624

 

你可能感兴趣的:(网络学习,gsoap,url)