C++访问WebService(gSoap方式和com组件方式)
2017-03-26 00:16
579 查看
一、 gSOAP访问WebService
gSOAP下载地址http://sourceforge.net/projects/gsoap2/
我下载的版本是gsoap-2.8
直接解压,如D:\private\code\gsoap-2.8
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,代码如下:
[cpp]
view plain
copy
#include <iostream>
#include <string>
#include <xstring>
// 名称空间映射表
#include "WeatherWSSoap.nsmap"
#include "soapWeatherWSSoapProxy.h"
using namespace std;
int main(void)
{
WeatherWSSoapProxy weatherwebservice;
// 获取近5天天气情况及城市信息
_WS__getWeather cityName;
_WS__getWeatherResponse weatherResponse;
wstring strAddress=L"广州";
cityName.theCityCode = const_cast<wchar_t*>(strAddress.c_str());
int result = weatherwebservice.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;
wcout.imbue(locale("chs"));
cout<<weatherString.size()<<endl;
for(itr = weatherString.begin(),itr_end = weatherString.end(); itr!=itr_end; ++itr)
{
wcout<<*itr<<endl;
}
cout<<endl;
}
system("pause");
return 0;
}
执行结果如下
参考:http://www.cnblogs.com/virtorld/archive/2012/11/16/2773395.html
http://blog.csdn.net/jq_develop/article/details/41982337
生成WeatherWebService.h文件
#include "WeatherWebService.h"
using namespace std;
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();
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,代码如下:
[cpp]
view plain
copy
#include <iostream>
#include <string>
#include <xstring>
// 名称空间映射表
#include "WeatherWSSoap.nsmap"
#include "soapWeatherWSSoapProxy.h"
using namespace std;
int main(void)
{
WeatherWSSoapProxy weatherwebservice;
// 获取近5天天气情况及城市信息
_WS__getWeather cityName;
_WS__getWeatherResponse weatherResponse;
wstring strAddress=L"广州";
cityName.theCityCode = const_cast<wchar_t*>(strAddress.c_str());
int result = weatherwebservice.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;
wcout.imbue(locale("chs"));
cout<<weatherString.size()<<endl;
for(itr = weatherString.begin(),itr_end = weatherString.end(); itr!=itr_end; ++itr)
{
wcout<<*itr<<endl;
}
cout<<endl;
}
system("pause");
return 0;
}
执行结果如下
参考: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();
相关文章推荐
- C++访问WebService(gSoap方式和com组件方式)
- C++访问WebService(gSoap方式和com组件方式)
- C++访问WebService(gSoap方式和com组件方式)
- C++访问WebService gSoap方式
- 利用gsoap将wsdl方式的WebService生成c/c++的接口
- php 访问 Webservice (基于linux c/c++ gSOAP )
- c++使用gsoap访问webservice
- c++访问webservice(gsoap简单使用)
- C++使用gsoap实现客户端访问webservice服务,概念知识普及
- linux下c/c++方式访问curl的帮助手册
- c++------------------------------------------------------------webservice_gsoap
- JAX-RPC学习笔记(3)-DII方式访问webservice
- gsoap写一个c++ webservice
- c/c++中利用gsoap调用webservice服务的一个诡异问题解决方法
- gsoap(C/C++调用webservice)
- 《C++第十二周实验报告1-1》----理解基类中成员的访问限定符和派生类的继承方式
- WebService 通过POST方式访问时候,因 URL 意外地以“/方法名”结束,请求格式无法识别 解决办法
- C++中如何使用gsoap开发WebService
- 标准C/C++程序通过gSOAP调用WebService