gSOAP是一個綁定SOAP/XML到C/C++語言的工具,使用它可以簡單快速地開發出SOAP/XML的服務器端和客戶端。由於 gSOAP具 有相當不錯的兼容性,通過gSOAP,我們就可以調用由Java, .Net, Delhpi, PHP等語言開發的SOAP服務,或者向它們提供SOAP服務。
(一)安裝
gSOAP的主頁是:
http://sourceforge.net/projects/gsoap2
下載解壓后,可以在gsoap\bin\win32里 找到wsdl2h.exe和soapcpp2.exe(另外 mac版本)。
根據實踐證明,windows下可以不用像https://www.genivia.com/downloads.html#releases文中所說如下圖編譯安裝,直接使用。
當然想重新編譯安裝也是可以的。具體步驟如下:
首先根據說明“Tip: when autoconf, automake, flex, bison, openssl-devel, and zlib-devel are installed you can run configure to build the gSOAP tools and libraries as follows:”
需要安裝以上幾個庫和工具,否則后面的編譯會出錯。
具體可以使用mingw-get.exe安裝
完成以上安裝后,接着可以進行gsoap編譯安裝
1、打開mingw32->msys目錄下的終端“msys.bat 美化mintty”,記得用美化mintty版,這樣可以使用右鍵copy。進入gsoap解壓后的目錄
2、執行./configure
3、執行make
4、安裝,執行make install
完成以后會將soapcpp2、wsdl2h工具及庫等安裝部署到以下目錄
根據實踐證明,生成的wsdl2h運行無法正常使用
所以不建議編譯安裝,如果實在要使用可以將gsoap源文件夾win32下的該程序拷貝過來替換。
最后是要在windows環境變量中加入以上安裝目錄中的bin、lib、include等目錄,記得實在不同的環境變量中哦。
如果不編譯安裝,而是直接使用gsoap源文件夾win32則環境變量中加入該目錄即可。
當然不加入以上lib、include亦可,只需在編程工具的工程項目中增加即可,netbeans、qt等工具都可以添加。
(二)使用demo
這個參考https://blog.csdn.net/qianqiuwanzi/article/details/24120455
(以經典的calc為例)
使用gsoap\bin\win32目錄中的兩個應用:soapcpp2.exe和wsdl2h.exe,生成客戶端和服務器端框架。
使用gSOAP生成客戶端和服務器端代碼框架。我們有兩種做法:
- 編寫WSDL,使用wsdl2h生成頭文件,再通過soapcpp2生成框架代碼;
- 編寫頭文件,使用soapcpp2生成框架代碼;

經常使用的一些指令如下:
-o filename.h 將wsdl轉化為filename.h頭文件。
-s 不生成STL代碼
-c 生成純C風格的頭文件,這將去除C++的一些特性
-n name 使用name代替默認前綴ns
-t filename.dat 使用filename.dat代替默認的typemap.dat文件
-zX 兼容之前的X版本


calc.h部分代碼如下
1 /******************************************************************************\ 2 * * 3 * Services * 4 * * 5 \******************************************************************************/ 6 7 8 //gsoap ns2 service name: calc 9 //gsoap ns2 service type: calcPortType 10 //gsoap ns2 service port: http://websrv.cs.fsu.edu/~engelen/calcserver.cgi 11 //gsoap ns2 service namespace: urn:calc 12 //gsoap ns2 service transport: http://schemas.xmlsoap.org/soap/http 13 14 /** @mainpage calc Definitions 15 16 @section calc_bindings Service Bindings 17 18 - @ref calc 19 20 @section calc_more More Information 21 22 - @ref page_notes "Notes" 23 24 - @ref page_XMLDataBinding "XML Data Binding" 25 26 - @ref SOAP_ENV__Header "SOAP Header Content" (when applicable) 27 28 - @ref SOAP_ENV__Detail "SOAP Fault Detail Content" (when applicable) 29 30 31 */ 32 33 /** 34 35 @page calc Binding "calc" 36 37 @section calc_service Service Documentation "calc" 38 gSOAP 2.7.9k generated service definition 39 40 @section calc_operations Operations of Binding "calc" 41 42 - @ref ns2__add 43 44 - @ref ns2__sub 45 46 - @ref ns2__mul 47 48 - @ref ns2__div 49 50 - @ref ns2__pow 51 52 @section calc_ports Default endpoints of Binding "calc" 53 54 - http://websrv.cs.fsu.edu/~engelen/calcserver.cgi 55 56 @note Use wsdl2h option -Nname to change the service binding prefix name 57 58 59 */ 60 //gsoap ns2 service method-protocol: add SOAP 61 //gsoap ns2 service method-style: add rpc 62 //gsoap ns2 service method-encoding: add http://schemas.xmlsoap.org/soap/encoding/ 63 //gsoap ns2 service method-action: add "" 64 //gsoap ns2 service method-output-action: add Response 65 int ns2__add( 66 double :a, ///< Input parameter, : unqualified name as per RPC encoding 67 double :b, ///< Input parameter, : unqualified name as per RPC encoding 68 double &:result ///< Output parameter, : unqualified name as per RPC encoding 69 ); 70 71 //gsoap ns2 service method-protocol: sub SOAP 72 //gsoap ns2 service method-style: sub rpc 73 //gsoap ns2 service method-encoding: sub http://schemas.xmlsoap.org/soap/encoding/ 74 //gsoap ns2 service method-action: sub "" 75 //gsoap ns2 service method-output-action: sub Response 76 int ns2__sub( 77 double :a, ///< Input parameter, : unqualified name as per RPC encoding 78 double :b, ///< Input parameter, : unqualified name as per RPC encoding 79 double &:result ///< Output parameter, : unqualified name as per RPC encoding 80 ); 81 82 //gsoap ns2 service method-protocol: mul SOAP 83 //gsoap ns2 service method-style: mul rpc 84 //gsoap ns2 service method-encoding: mul http://schemas.xmlsoap.org/soap/encoding/ 85 //gsoap ns2 service method-action: mul "" 86 //gsoap ns2 service method-output-action: mul Response 87 int ns2__mul( 88 double :a, ///< Input parameter, : unqualified name as per RPC encoding 89 double :b, ///< Input parameter, : unqualified name as per RPC encoding 90 double &:result ///< Output parameter, : unqualified name as per RPC encoding 91 ); 92 93 //gsoap ns2 service method-protocol: div SOAP 94 //gsoap ns2 service method-style: div rpc 95 //gsoap ns2 service method-encoding: div http://schemas.xmlsoap.org/soap/encoding/ 96 //gsoap ns2 service method-action: div "" 97 //gsoap ns2 service method-output-action: div Response 98 int ns2__div( 99 double :a, ///< Input parameter, : unqualified name as per RPC encoding 100 double :b, ///< Input parameter, : unqualified name as per RPC encoding 101 double &:result ///< Output parameter, : unqualified name as per RPC encoding 102 ); 103 104 //gsoap ns2 service method-protocol: pow SOAP 105 //gsoap ns2 service method-style: pow rpc 106 //gsoap ns2 service method-encoding: pow http://schemas.xmlsoap.org/soap/encoding/ 107 //gsoap ns2 service method-action: pow "" 108 //gsoap ns2 service method-output-action: pow Response 109 int ns2__pow( 110 double :a, ///< Input parameter, : unqualified name as per RPC encoding 111 double :b, ///< Input parameter, : unqualified name as per RPC encoding 112 double &:result ///< Output parameter, : unqualified name as per RPC encoding 113 );
接着生成源文件:
soapcpp2 頭文件 常用選項 -C 僅生成客戶端代碼 -S 僅生成服務器端代碼 -L 不要產生soapClientLib.c和soapServerLib.c文件 -c 產生純C代碼,否則是C++代碼(與頭文件有關) -I 指定import路徑(見上文) -x 不要產生XML示例文件 -i 生成C++包裝,客戶端為xxxxProxy.h(.cpp),服務器端為xxxxService.h(.cpp)。--wsj本人建議加上

在cmd命令行下進入工程目錄,執行:
soapcpp2 -L -w -x -i -I D:\tools\gsoap-2.8\gsoap\import calc.h



通過netbeans創建工程項目soap_calc(client)
並將生成的soapC.cpp、soapH.h、soapStub.h、soapcalcProxy.h、soapcalcProxy.cpp、calc.nsmap、stdsoap2.h和stdsoap2.cpp文件加入到工程,其中stdsoap2.h和stdsoap2.cpp為gsoap文件夾中的文件拷貝過來即可,如果是c語言則stdsoap2.cpp替換為stdsoap2.c。
客戶端代碼如下
1 #include <cstdlib> 2 #include "calc.nsmap"//一定要加,不要忘記 3 #include "soapcalcProxy.h" 4 5 using namespace std; 6 7 /* 8 * 9 */ 10 int main(int argc, char** argv) { 11 calcProxy calc_clnt; 12 double add; 13 int ret=calc_clnt.add(1,2,add); 14 if(ret==SOAP_OK) { 15 printf("add=%lf", add); 16 }else 17 { 18 printf("err_code=%d", ret); 19 } 20 return 0; 21 }
運行結果
編譯工程時特別要注意加上其它編譯項和庫
netbeans中在
中設置即可。
qtcreator只需在pro文件里添加即可
注意事項(關鍵):---wsj
當直接使用 soapcpp2 頭文件 生成文件時,如果頭文件的命名是
int downloadCAGCtrl(std::string strXMLParams,std::string *strXMLResult);時
使用soapcpp2 I2Server.h命令只能生成以下文件,無法生成wsdl、xml類型文件,這個wsdl文件在此頭文件生成webservice服務端的時候很關鍵,在webservice調用的時候要使用。
這時候,就要將頭文件內容修改下:
int ns__downloadCAGCtrl(std::string strXMLParams,std::string *strXMLResult);
對了就是在函數名前面加了命名空間名字,通常用默認的ns__
同樣的命令:soapcpp2 I2Server.h 結果如下
服務端的話,建議采用命令soapcpp2 -L -S -i I2Server.h
結果: