- 下载gsoap下载地址 https://sourceforge.net/projects/gsoap2/files/,我下载的是gsoap_2.8.87.zip
- 解压后依次进入 gsoap-2.8/gsoap/bin/win32 目录,新建一个start.bat文件,将以下内容复制粘贴进去
echo 生成头文件Service.h
wsdl2h.exe -o Service.h http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL
echo 生成其它soap文件支持
soapcpp2.exe -I../../import -x -L -i -C Service.h -1
del OutFile /s /Q
MD OutFile
echo 拷贝文件
copy ..\..\stdsoap2.h OutFile\
copy ..\..\stdsoap2.cpp OutFile\
copy *.h OutFile\
copy *.cpp OutFile\
copy *.nsmap OutFile\
echo 删除临时文件
del /f/q .\*.h
del /f/q .\*.cpp
del /f/q .\*.nsmap
pause
- 修改start.bat文件中wsdl服务链接地址,这里以天气预报webservice服务地址http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL进行举例
- wsdl2h.exe和soapcpp2.exe生成代理类程序入参参数根据实际情况可适量进行调整
wsdl2h.exe参数常用选项
-o filename.h 将wsdl转化为filename.h头文件
-n name 使用name命名空间前缀代替默认的前缀ns
-c 产生纯C代码,否则是C++代码
-s 不要使用STL代码
-t filename.dat 使用filename.dat代替默认的typemap.dat文件
-e 禁止为enum成员加上名空间前缀
soapcpp2.exe参数常用选项
-C 仅生成客户端代码
-S 仅生成服务器端代码
-L 不要产生soapClientLib.c和soapServerLib.c文件
-c 产生纯C代码,否则是C++代码(与头文件有关)
-I 指定import路径(见上文)
-x 不要产生XML示例文件
-i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)
-p name 修改文件名前缀,代替soap
-q name 指定代理类和对象使用的名空间name,包含文件名前缀
- 双击运行start.bat,执行完成后会在当前目录生成OutFile文件夹,里面的所有文件即是我们需要加入到VS工程中的文件
- 以Visual Studio 2008进行举例说明,调用该服务的getSupportCity接口,入参byProvinceName输入:湖北,获取返回结果
// TestWeatherWebService.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "TestWeatherWebService.h"
#include "WeatherWebServiceSoap.nsmap"
#include "soapWeatherWebServiceSoapProxy.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
string UTF8ToGBK(const std::string& strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
unsigned short * wszGBK = new unsigned short[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, (LPWSTR)wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
delete [] szGBK;
szGBK = NULL;
delete [] wszGBK;
wszGBK = NULL;
return strTemp;
}
string GBKToUTF8(const std::string& strGBK)
{
string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete [] str1;
str1 = NULL;
delete [] str2;
str2 = NULL;
return strOutUTF8;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
}
WeatherWebServiceSoapProxy service("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx", SOAP_C_UTFSTRING);
service.connect_timeout = 10;//连接超时时间,正值以秒为单位,负值以微秒为单位
service.send_timeout = 10;//发送超时时间,正值以秒为单位,负值以微秒为单位
service.recv_timeout = 10;//接收超时时间,正值以秒为单位,负值以微秒为单位
//service.accept_timeout;
//service.transfer_timeout;
_ns1__getSupportCity input;
_ns1__getSupportCityResponse output;
string strProvinceName = "湖北";
cout << "input:" << strProvinceName << endl;
string strUtfProvinceName = GBKToUTF8(strProvinceName);
input.byProvinceName = &strUtfProvinceName;
int soap = service.getSupportCity(&input, output);
if (SOAP_OK == soap && NULL != output.getSupportCityResult)
{
for (int i = 0; i < output.getSupportCityResult->string.size(); i++)
{
cout << "output:" << UTF8ToGBK(output.getSupportCityResult->string.at(i)) << endl;
}
}
else
{
cout << "error:"<<service.error<<",faultcode:"<<*soap_faultcode(&service)<<",faultstr:"<<*soap_faultstring(&service) << endl;
}
system("pause");
return nRetCode;
}
- 工程部署:
- 执行结果:
wsdl2h链接多个soap服务
wsdl2h.exe -nName -o Service.h http://127.0.0.1:8808/1?wsdl http://127.0.0.1:8808/2?wsdl