C++訪問WebService(gSoap方式和com組件方式)


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

 

 


免責聲明!

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



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