一、 gSOAP通路WebService
1. 下載下傳gSOAP
gSOAP 2.7.17 版下載下傳位址http://sourceforge.net/projects/gsoap2/)
2. 安裝gSOAP
解壓下載下傳的gsoap_2.7.17.zip,假設該路徑為F:\WebService\gsoap-2.7
3. 通過WSDL生成C++頭檔案
3.1、通過WSDL生成通路接口
在 F:\WebService\gsoap-2.7\gsoap\bin\win32目錄下建一個空的頭檔案WebService.h;再建立一個字元轉換規則檔案wsmap.dat,檔案内容為xsd__string = | std::wstring | wchar_t*
那麼SOAP/XML中的string将轉換成std::wstrin或wchar_t*,這樣能更好地支援中文。
啟動cmd,進入到F:\WebService\gsoap-2.7\gsoap\bin\win32目錄,調用wsdl2h.exe生成頭檔案接口定義,指令為:
wsdl2h -o WebService.h -n WS -t wsmap.dat http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL

- -o 檔案名,指定輸出頭檔案
- -n 名空間字首 代替預設的ns
- -c 産生純C代碼,否則是C++代碼
- -s 不要使用STL代碼
- -t 檔案名,指定type map檔案,預設為typemap.dat
- -e 禁止為enum成員加上名空間字首
3.2、解析WebService.h,生成存根程式
在指令行輸入soapcpp2 -C WebService.h -I F:\WebService\gsoap-2.7\gsoap\import
最後提示成功,在F:\WebService\gsoap-2.7\gsoap\bin\win32目錄中生成系列檔案如下:
- -C 僅生成用戶端代碼
- -S 僅生成伺服器端代碼
- -L 不要産生soapClientLib.c和soapServerLib.c檔案
- -c 産生純C代碼,否則是C++代碼(與頭檔案有關)
- -I 指定import路徑(此項是必要的,因前面為指定-s)
- -x 不要産生XML示例檔案
- -i生成C++包裝,用戶端為xxxxProxy.h(.cpp),伺服器端為xxxxService.h(.cpp)。
4. 建立工程
4.1、頭檔案
将生成的soapC.cpp、soapClient.cpp、soapH.h、soapStub.h、soapWeatherWSSoapProxy.h、WeatherWSSoap.nsmap、stdsoap2.h和stdsoap2.cpp檔案加入到工程
4.2、命名空間
在工程的頭檔案中加入#include “WeatherWSSoap.nsmap”,否則會有命名空間編譯出錯的問題
4.3、代碼示例
#include <iostream>
#include <string>
// 名稱空間映射表
#include "WeatherWSSoap.nsmap"
#include "soapWeatherWSSoapProxy.h"
using namespace std;
main函數:
// // 代理類對象
WeatherWSSoap weatherwebservice;
// 擷取近5天天氣情況及城市資訊
_ns1__getWeather cityName;
_ns1__getWeatherResponse weatherResponse;
cityName.theCityCode = L"北京";
int result = weatherwebservice.__ns2__getWeather(&cityName, &weatherResponse);
if(SOAP_OK == result)
{
vector<wstring> weatherString = weatherResponse.getWeatherResult->string;
vector<wstring>::iterator itr;
vector<wstring>::iterator itr_end;
cout<<"近5天天氣情況及城市資訊:"<<endl;
for(itr = weatherString.begin(),itr_end = weatherString.end(); itr!=itr_end; ++itr)
{
wcout<<*itr<<endl;
}
cout<<endl;
}
二、 非托管com元件通路WebService
在Visual Studio 2008以及以後版本中,微軟停止了非托管C++的直接WebService引用。不過ATL Server代碼已經托管到開源網站上,我們可以找到ATL Server的源代碼,編譯出Sproxy.exe,這個工具可以根據wsdl檔案來生成非托管的代理類。這個代理類還需要配合一些頭檔案才能一起使用,這個相關的頭檔案都包含在ATL Server 的源代碼内。
1. 準備sproxy.exe工具
在vs2008以前的版本,比如vs2005,本身就帶有這個指令,但在vs2008版,已經把它給去除了。需要去http://atlserver.codeplex.com/下載下傳ATL_Server源代碼并編譯産生sproxy.exe工具。
2. 生成代理類
啟動cmd,進入sproxy.exe目錄,執行sproxy.exe / wsdl http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
生成WeatherWebService.h檔案
3. 建立工程
3.1、頭檔案
#include "iostream"
#include "WeatherWebService.h"
using namespace std;
3.2、代碼示例
// 設定中文區域
setlocale(LC_ALL,"chs");
CoInitialize(NULL);
HRESULT hr = S_OK;
WeatherWebService::CWeatherWebServiceT<CSoapSocketClientT<>>* mWeatherWS = new WeatherWebService::CWeatherWebServiceT<CSoapSocketClientT<>>;
CComBSTR cityName = "北京";
BSTR* weatherOut;
int weatherSize;
// 擷取天氣
hr = mWeatherWS->getWeatherbyCityName(cityName,(BSTR**)&weatherOut,&weatherSize);
if(FAILED(hr))
{
cout<<"getWeather fail!"<<endl;
}
else
{
for (int i=0;i<weatherSize;i++)
{
wcout<<weatherOut[i]<<endl;
}
}
if (mWeatherWS != NULL)
delete mWeatherWS;
CoUninitialize();