首先需要下载gsoap_2.8.14.zip工具,该工具的主要作用是将服务提供者的WSDL(Web Service描述语言)转化为C++头文件和源文件。
1、生成头文件
使用wsdl2h命令生成头文件,
wsdl2h -o WeatherWS.hhttp://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
wsdl2h命令参数见如下:
· -o 文件名,指定输出头文件
· -n 名空间前缀 代替默认的ns
· -c 产生纯C代码,否则是C++代码
· -s 不要使用STL代码
· -t文件名,指定type map文件,默认为typemap.dat
· -e 禁止为enum成员加上名空间前缀
执行这个命令之后,界面如下,表明已经生成了头文件
2、生成程序调用的头文件及源文件
使用soapcpp2命令,生成CPP文件及程序调用的头文件。
soapcpp2 -C WeatherWS.h -I D:\study\webservices\gsoap-2.8\gsoap\import
命令参数如下:
· -C 仅生成客户端代码
· -S 仅生成服务器端代码
· -L 不要产生soapClientLib.c和soapServerLib.c文件
· -c 产生纯C代码,否则是C++代码(与头文件有关)
· -I 指定import路径(此项是必要的,因前面为指定-s)
· -x 不要产生XML示例文件
· -i生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。
执行这个命令之后,有如下界面:
生成的所有的文件如下:
以上的这两个命令能够直接使用,是因为已经将其路径添加到环境变量中。
选中如下的5个文件(soapC.cpp,soapClient.cpp, soapH.h,soapStub.h,WeatherWSSoap.nsmap),复制到工程项目中。
同时还要复制如下图中的两个文件(stdsoap2.cpp,stdsoap2.h)到工程项目中,这两个文件是在gSOAP的安装目录下。
在工程中添加这七个文件,如下图选中的。
如下为添加七个文件之后的项目结构
在如下的代码框架中,添加调用Web Services接口。如下代码若无法编译通过,则需设置编译选项中的“不使用预编译”
客户端调用的所有接口都在如下的截图中。
如下为程序调用的代码,代码中的user_id为在webxml.com.cn网站上注册时提供的,若没有这个ID,可能调用次数会受到限制。
// WeatherWS.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include "soapH.h"
#include "WeatherWSSoap.nsmap"
using namespacestd;
void MultiByte2WideChar(char* src, const wchar_t*dst)
{
if (NULL ==src ||NULL ==dst)
{
return;
}
int srcSize = strlen(src);
int iLen = MultiByteToWideChar(CP_UTF8, 0,src,srcSize,NULL, 0);
if (0 == iLen)
{
return;
}
MultiByteToWideChar(CP_UTF8, 0,src,strlen(src), (LPWSTR)dst,iLen * 2);
}
int _tmain(intargc,_TCHAR*argv[])
{
struct soap soap;
soap_init(&soap);
soap_set_mode(&soap,SOAP_C_UTFSTRING);
soap.mode |=SOAP_C_UTFSTRING;
//----在以下添加调用Webservices代码
_ns1__getRegionProvince regionProvince;
_ns1__getRegionProvinceResponseregionProvinceResponse;
_ns1__getSupportCityStringsupportCity;
_ns1__getSupportCityStringResponsesupportCityResponse;
_ns1__getWeather weather;
_ns1__getWeatherResponse weatherResponse;
//用户ID
std::stringuser_id ="a5252ee7eac64823888fee66ec500953";
//获取省
if(SOAP_OK ==soap_call___ns1__getRegionProvince(&soap,NULL,NULL,®ionProvince,®ionProvinceResponse))
{
int nRegions = (regionProvinceResponse.getRegionProvinceResult->string).size();
for (inti = 0;i <nRegions; ++i)
{
char szRegion[256] = {0};
wchar_t szWRegion[256] = {0};
char szRegionBuff[256] = {0};
strncpy(szRegion,regionProvinceResponse.getRegionProvinceResult->string[i].c_str(),regionProvinceResponse.getRegionProvinceResult->string[i].length());
MultiByte2WideChar(szRegion,szWRegion);
WideCharToMultiByte(CP_ACP,0,szWRegion,-1,szRegionBuff,255,NULL,FALSE);
printf("%s\n",szRegionBuff);
char* pB = strstr(szRegionBuff,",");
char RegionCode[32] = {0};
strncpy(RegionCode,pB+1,31);
std::stringregionCode =RegionCode;
supportCity.theRegionCode = ®ionCode;
//获取市
if(SOAP_OK ==soap_call___ns1__getSupportCityString(&soap,NULL,NULL,&supportCity,&supportCityResponse))
{
int nCitys = (supportCityResponse.getSupportCityStringResult->string).size();
for (intj = 0;j <nCitys ; ++j)
{
char szCity[256] = {0};
wchar_t szWCity[256] = {0};
char szCityBuff[256] = {0};
strncpy(szCity,supportCityResponse.getSupportCityStringResult->string[j].c_str(),supportCityResponse.getSupportCityStringResult->string[j].length());
MultiByte2WideChar(szCity,szWCity);
WideCharToMultiByte(CP_ACP,0,szWCity,-1,szCityBuff,255,NULL,FALSE);
printf("%s\n",szCityBuff);
char* pB = strstr(szCityBuff,",");
char CityCode[32] = {0};
strncpy(CityCode,pB+1,31);
std::stringcityCode =CityCode;
weather.theCityCode = &cityCode;
weather.theUserID = &user_id;
//获取城市的天气预报
if(SOAP_OK ==soap_call___ns1__getWeather(&soap,NULL,NULL,&weather,&weatherResponse))
{
int cnt = (weatherResponse.getWeatherResult->string).size();
for (intk = 0;k < cnt; k++)
{
wchar_t buffWeather[12560] = {0};
char szBuffWeather[12560] = {0};
strncpy(szBuffWeather,weatherResponse.getWeatherResult->string[k].c_str(),12550);
MultiByte2WideChar(szBuffWeather,buffWeather);
char szMulWeather[12560] = {0};
WideCharToMultiByte(CP_ACP,0,buffWeather,-1,szMulWeather,12550,NULL,FALSE);
printf("%s\n",szMulWeather);
}
}
}
}
}
}
soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap);
return 0;
}
运行结果截图如下:
很多图片不能直接从WORD复制过来,我会上传这一份文档的。___今天重新编辑了一下这个文章,把所有图片都上传了。
文档链接http://download.youkuaiyun.com/detail/cabmail/8847119