C++访问WebService(gSoap方式和com组件方式)

一、             gSOAP访问WebService

1.      下载gSOAP

gSOAP下载地址http://sourceforge.net/projects/gsoap2/

我下载的版本是gsoap-2.8

2.      安装gSOAP

直接解压,如D:\private\code\gsoap-2.8

3.      通过WSDL生成C++头文件

3.1 通过wsdl生成访问接口
  • 在目录D:\private\code\gsoap-2.8\gsoap\bin\win32下新建一个空的头文件WebService.h;
  • 建立一个字符转换规则文件wsmap.dat,文件内容为xsd__string = | std::wstring | wchar_t*,那么SOAP/XML中的string将转换成std::wstrin或wchar_t*,这样能更好地支持中文;
  • 启动cmd,进入到D:\private\code\gsoap-2.8\gsoap\bin\win32目录,调用wsdl2h.exe生成头文件接口定义,命令为:
    wsdl2h -o WebService.h -N WS -n WS -R -t wsmap.dat http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL;

3.2 解析WebService.h,生成存根程序
仍然是目录D:\private\code\gsoap-2.8\gsoap\bin\win32下,在命令行输入soapcpp2 -C WebService.h -i -I D:\private\code\gsoap-2.8\gsoap\import



成功后目录D:\private\code\gsoap-2.8\gsoap\bin\win32文件列表如下:

4.建立win32工程

  • 建立一个空的win32工程gsoapWeather;
  • 将生成的soapC.cpp、soapClient.cpp、soapH.h、soapStub.h、soapWeatherWSSoapProxy.h、WeatherWSSoap.nsmap、stdsoap2.h和stdsoap2.cpp文件加入到工程;
  • 添加一个cpp文件gsoapWeather.cpp,代码如下:
    1. #include <iostream>  
    2. #include <string>  
    3. #include <xstring>  
    4. // 名称空间映射表  
    5. #include "WeatherWSSoap.nsmap"  
    6. #include "soapWeatherWSSoapProxy.h"  
    7. using namespace std;  
    8.   
    9. int main(void)  
    10. {  
    11.     WeatherWSSoapProxy weatherwebservice;  
    12.     // 获取近5天天气情况及城市信息  
    13.     _WS__getWeather cityName;  
    14.     _WS__getWeatherResponse weatherResponse;  
    15.     wstring strAddress=L"广州";  
    16.     cityName.theCityCode = const_cast<wchar_t*>(strAddress.c_str());  
    17.   
    18.     int result = weatherwebservice.getWeather(&cityName, weatherResponse);  
    19.     if(SOAP_OK == result)  
    20.     {  
    21.         vector<wstring> weatherString = weatherResponse.getWeatherResult->string;  
    22.         vector<wstring>::iterator itr;  
    23.         vector<wstring>::iterator itr_end;  
    24.         cout<<"近5天天气情况及城市信息:"<<endl;  
    25.         wcout.imbue(locale("chs"));  
    26.         cout<<weatherString.size()<<endl;  
    27.         for(itr = weatherString.begin(),itr_end = weatherString.end(); itr!=itr_end; ++itr)  
    28.         {  
    29.             wcout<<*itr<<endl;  
    30.         }  
    31.         cout<<endl;  
    32.     }  
    33.   
    34.     system("pause");  
    35.     return 0;  
    36. }  
    执行结果如下
参考:http://www.cnblogs.com/virtorld/archive/2012/11/16/2773395.html
http://blog.csdn.net/jq_develop/article/details/41982337

二、             非托管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();

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值