C++訪問WebService gSoap方式


一、             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;

  }

 

 第二種方式:

在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工具。

1.1先把下載好的文件ATL_Server_Source_and_Headers_9_0_70425_Alpha.rar解壓出來,然后進入文件夾把include內的文件全部拷貝到vs2008的默認包含文件夾內,如我的路徑:“D:\ProgramFiles\Microsoft Visual Studio 9.0\VC\include”,拷貝之前最好先把原來的include文件夾備份一下。

1.2之后用進入“ATL_Server_Source_and_Headers_9_0_70425_Alpha\source\SProxy\”,用vs2008打開“sproxy.sln”文件,並在vs2008提示下進行版本轉換。

1.3編譯運行,生成sproxy.exe文件。

 

2.為了測試webservice服務,在網上找了一個提供webservice的網站:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx它提供天氣預報服務。它的WSDL文件在這個:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL,這是一個xml文件,把這個文件保存為本地文件,名字叫WeatherWS.wsdl。為了方便,假定這個文件和sproxy.exe放在同一個目錄下,執行命令

sproxy.exe /wsdl WeatherWS.wsdl

sproxy.exe /wsdl http://網址

就會在同目錄下生成WeatherWS.h文件。

3. 打開vs2008,建立一個MFC項目WebService,在WebServiceDlg.cpp文件中添加

#include "WeatherWS.h"

using namespace WeatherWS;

在按鈕點擊事件中加入下面的測試代碼:

void CWebServiceDlg::OnBnClickedButton1()

{

       // TODO: 在此添加控件通知處理程序代碼

       CoInitialize(NULL);

       HRESULT hr = S_OK;

       CWeatherWST<CSoapSocketClientT<>>* m_srv = new CWeatherWST<CSoapSocketClientT<>>;

       CComBSTR cityCode =""; //參數為空默認返回上海的天氣情況 

       CComBSTR userId ="";

       CComBSTR * bstrOut;

       int size;

       hr =m_srv->getWeather(cityCode, userId, (BSTR * *)&bstrOut, &size);

       if(FAILED(hr))

       {

              MessageBox(L"調用失敗");

       }

       else

       {

              MessageBox(L"調用成功");

       }

       if (m_srv != NULL)

              delete m_srv;

       CoUninitialize();

}

 

以上資料整理來自網絡,感謝原作者。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM