1、保存WSDL:打开WSDL,直接保存,将后缀改为WSDL即可。
2、在win32目录下新建一个文件”wsmp.dat“,文件内容为xsd__string = | std::wstring | wchar_t*
3、生成头文件:wsdl2h -s -t wsmap.dat WeatherWS.wsdl
- -o 文件名,指定输出头文件
- -n 名空间前缀 代替默认的ns
- -c 产生纯C代码,否则是C++代码
- -s 不要使用STL代码
- -t 文件名,指定type map文件,默认为typemap.dat
- -e 禁止为enum成员加上名空间前缀
4、解析头文件,生成存根程序 ,需要引入依赖项。
- -C 仅生成客户端代码
- -S 仅生成服务器端代码
- -L 不要产生soapClientLib.c和soapServerLib.c文件
- -c 产生纯C代码,否则是C++代码(与头文件有关)
- -I 指定import路径(此项是必要的,因前面为指定-s)
- -x 不要产生XML示例文件
- -i生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。
soapcpp2 -i -C -x -L WeatherWS.h -I D:\gsoap_2.8.108\gsoap-2.8\gsoap\import
5、 将生成的头文件和源文件添加到VS工程中,另外还有stdsoap2.h和stdsoap2.cpp;
6、示例:
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include < string >
#include <iostream>
#include <iostream>
//包含soap头文件
#include "soapH.h"
#include "soapStub.h"
#include "WeatherWSSoap.nsmap"
#include "soapWeatherWSSoapProxy.h"
using namespace std;
void main(int argc, char **argv)
{
//WebService的请求地址
const char* web_url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
//soap接口
WeatherWSSoapProxy soap(SOAP_C_UTFSTRING);
//构造输入参数
_ns1__getWeather city_name;
city_name.theCityCode = L"";
city_name.theUserID = L"";
//输出参数
_ns1__getWeatherResponse weather_res;
//调用接口方法getWeather
int xlt = soap.getWeather(web_url, NULL, &city_name, weather_res);
//判断接口返回值, SOAPOK表示成功
if (xlt == SOAP_OK)
{
// 获取返回结果
ns1__ArrayOfString* aos = weather_res.getWeatherResult;
// 打印返回结果
int count = aos->__sizestring;
for (int i = 0; i < count; i++)
{
std::wstring a = (aos->string)[i];
wcout.imbue(locale("chs"));
wcout << a.c_str() << endl;
}
}
getchar();
}