1. 程式人生 > >gsoap+URL自動生成標頭檔案與客戶端程式碼流程

gsoap+URL自動生成標頭檔案與客戶端程式碼流程

1、下載gsoap程式碼下載連結

    下載以後解壓,進入\gsoap-2.8\gsoap-2.8\gsoap\bin\win32這個目錄下面有兩個執行檔案soapcpp2.exe 與wsdl2h.exe

    wsdl2h.exe這個是生成標頭檔案的

    soapcpp2.exe這個是生成程式碼的。

2、以天氣預報的url為例生成標頭檔案

3、cmd命令進入wsdl2h.exe這個所在的目錄然後執行

命令解釋: wsdl2h -o 標頭檔案名 WSDL檔名或URL wsdl2h常用選項 -o 檔名,指定輸出標頭檔案 -n 名空間字首 代替預設的ns -c 產生純C程式碼,否則是C++程式碼 -s 不要使用STL程式碼 -t 檔名,指定type map檔案,預設為typemap.dat -e 禁止為enum成員加上名空間字首

生成test.h標頭檔案,正常執行列印如下:

**  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<path> (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字符集如下面兩個圖。

然後匯入專案,在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這個目錄下。