VS2008及之后的版本已經不支持使用C++開發WEBService服務了,如果要在VS上開發WEBService,需要使用C#開發語言。
一、gSOAP簡介
gSOAP編譯工具提供了一個基於SOAP/XML 的C/C++ 語言實現,從而讓C/C++語言開發WebService客戶或服務端的程序工作變得輕松了很多。絕大多數的C++Web服務工具包提供一組API函數類庫來處理特定的SOAP數據結構,這樣就使得用戶必須改變程序結構來適應相關的類庫。與之相反,gSOAP利用編譯器技術提供了一組透明化的SOAP API,並將與開發無關的SOAP實現細節相關的內容對用戶隱藏起來。gSOAP的編譯器能夠自動的將用戶定義的本地化的C或C++數據類型轉變為符合XML語法的數據結構,反之亦然。這樣,只用一組簡單的API就將用戶從SOAP細節實現工作中解脫了出來,從則可以專注於應用程序邏輯的實現工作。
gSOAP編譯器可以集成C/C++和Fortran代碼(通過一個Fortran到C的接口),嵌入式系統,其他SOAP程序提供的實時軟件的資源和信息;可以跨越多個操作系統,語言環境以及在防火牆后的不同組織。
二、gSOAP的環境搭建與C++客戶端訪問WebService的操作實例
1 gSOAP的安裝:
下載gSOAP:https://sourceforge.net/projects/gsoap2/
https://sourceforge.net/projects/gsoap2/files/gsoap-2.8/gsoap_2.8.46.zip/download
解壓下載包(本例為gsoap_2.7.16.zip文件)到任意目錄,本例為E:\WebServiceClientTest
2 根據WSDL生成相應的.h和.cpp文件。
2.1 獲取WSDL文件
如通過瀏覽器打開WSDL的網址,點擊另存為,將網頁存在WSDL格式的文件(本例中命名為HelloWorld.wsdl)。
2.2 gsoap-2.7\gsoap\bin\win32 下的wsdl2h.exe、soapcpp2.exe文件
其中wsdl2.h.exe用於將WSDL文件生成C++的.h頭文件,
soapcpp2.exe用於將頭文件生成為.cpp源文件。
PS:wsdl2h命令的常用選項
- -o 文件名,指定輸出頭文件
- -n 名空間前綴 代替默認的ns
- -c 產生純C代碼,否則是C++代碼
- -s 不要使用STL代碼
- -t 文件名,指定type map文件,默認為typemap.dat
- -e 禁止為enum成員加上名空間前綴
2.3:將HelloWorld.wsdl 復制到gsoap-2.7\gsoap\bin\win32目錄下
public class HelloWorld{
public String sayHelloWorld (String name);
}
2.4 復制命令行所需的文件
將gsoap-2.7\gsoap\import\stlvector.h 和 stl.h、 stldeque.h、stllist.h、stlset.h、soap12.h文件復制到 gsoap-2.7\gsoap\bin\win32 目錄下。
將gsoap-2.7\gsoap\stdsoap2.h 和stdsoap2.cpp(如果是c語言的話則拷貝stdsoap2.c)復制到gsoap-2.7\gsoap\bin\win32目錄下。
2.5 生成頭文件
根據wsdl文件生成.h文件:打開cmd命令行,cd到gsoap-2.7\gsoap\bin\win32目錄,使用如下命令生成.h文件:
wsdl2h.exe –o HelloWorld.h HelloWorld.wsdl
2.6 根據.h頭文件生成.cpp源文件
仍然在上述目錄,執行命令為
soapcpp2.exe -C HelloWorld.h
3 C++客戶端調用WebService的實現
3.1 使用Visual Studio工具創建控制台應用程序webServiceClientTest,並創建一個文件夾gsoap。
3.2 將HelloWorld.h soapH.h soapStub.h stdsoap2.h soapC.cpp soapClient.cpp stdsoap2.cpp HelloWorldHttpBinding.nsmap文件復制到項目文件夾gsoap下面,並將此文件夾下的所有文件添加到項目webServiceClientTest。
同時在soapClient.cpp、soapC.cpp、stdsoap2.cpp 三個文件的頭一行增加 #include "stdafx.h"
實現的示例代碼如下:

1 // WebServiceClientTest.cpp : 定義控制台應用程序的入口點。 2 // 3 4 #include "stdafx.h" 5 #include "gsoap/service1soap.nsmap" 6 #include "stdio.h" 7 8 int _tmain(int argc, _TCHAR* argv[]) 9 { 10 //soap客戶端 11 struct soap clientSoap; 12 13 //WEBService調用對象 14 class _ns1__HelloWorld ws_HelloWorldObject; 15 class _ns1__Sum ws_sumObject; 16 17 18 //WEBService返回對象 19 class _ns1__HelloWorldResponse ws_HelloWorldResponseObject; 20 class _ns1__SumResponse ws_sum_ResponseObject; 21 22 ///SOAP 初始化 23 soap_init(&clientSoap); 24 25 ///調用函數的參數賦值 26 int sum_a = 120; 27 int sum_b = 12; 28 printf("請分別輸入需要相加的兩個數字,確認后請按回車鍵\r\n"); 29 scanf("%d\r\n%d",&sum_a, &sum_b); 30 31 ws_sumObject.a = sum_a; 32 ws_sumObject.b = sum_b; 33 34 35 ///發送WEBService請求,並獲得返回結果 36 int nResult = soap_call___ns1__HelloWorld(&clientSoap, NULL, NULL, &ws_HelloWorldObject, ws_HelloWorldResponseObject); 37 int sumNResult = soap_call___ns1__Sum(&clientSoap, NULL, NULL, &ws_sumObject, ws_sum_ResponseObject); 38 39 //操作成功 40 if(SOAP_OK == nResult) 41 { 42 //輸出返回結果 43 std::string strResult = "返回結果為: "; 44 strResult.append(*(ws_HelloWorldResponseObject.HelloWorldResult)); 45 printf("%s\r\n", strResult.c_str()); 46 } 47 if(SOAP_OK == sumNResult) 48 { 49 //輸出返回結果 50 std::string strResult = "返回結果為: "; 51 int sumR = ws_sum_ResponseObject.SumResult; 52 printf("%s%d\r\n", strResult.c_str(), sumR); 53 54 } 55 56 //關閉soap 57 soap_end(&clientSoap); 58 59 getchar(); 60 getchar(); 61 return 0; 62 }
參考:
http://blog.sina.com.cn/s/blog_4b44e1c00101bqim.html
http://www.cnblogs.com/LiGengMing/p/5594314.html