您的位置:首页 > 其它

gsoap生成webservice调用客户端接口

2013-07-05 13:04 357 查看
1.下载gsoap2.8

2.运行

[plain] view
plaincopy

wsdl2h.exe -o XXX.h XXX.wsdl

wsdl文件可以是本地文件,也可以是服务器的wsdl,比如http://192.168.0.122:3333/ws/uss?wsdl

3.生成客户端代码

[plain] view
plaincopy

soapcpp2.exe -L -x -C XXX.h -I .\gsoap-2.8\gsoap\import

4.如果有多个服务,那么就将要使用命名空间

[plain] view
plaincopy

wsdl2h -nns别名 -N服务命名空间 -o XXX.h XXX.wsdl

wsdl2h -nuseraddns -NUserAddWS -oUserAddWebService.h userAddWebService.wsdl

wsdl2h -nuserloginns -NUserLoginWS -oUserLoginWebService.h userLoginWebService.wsdl

手动将生成的.h合并为一个新文件,比如上面两个User*.h,合并为新文件all.h,对照着很容易看出不同,把命名空间,类声明等合在一起就行了

[plain] view
plaincopy

soap2cpp -LCx -pCMSWS All.h -I ../../import

拷贝gsoap2.8目录下的stdsoap2.h,stdsoap2.cpp到程序目录,并修改stdsoap2.cpp文件,将其中一个.nsmap文件包含进去

[plain] view
plaincopy

#include "xxx.nsmap"

5.传输中文

调用方法转为utf-8传输soap_set_mode( soap*, SOAP_C_UTFSTRING )

如果用qt的QString将转换

[cpp] view
plaincopy

std::string CUIUtils::convertQStrToUTF8Str(const QString& value) {

QByteArray arr = value.toUtf8();

return std::string(arr.data());

}

QString CUIUtils::convertUTF8StrToQStr(const std::string& value) {

return QString::fromUtf8(value.c_str());

}

例子

[cpp] view
plaincopy

int CUserDetailInfoWebServiceImpl::getUserInfo(const char* address, CUserDetailInfo* userDetailInfo)

{

UserDetailInfoWebServiceCXFImplServiceSoapBinding webServiceBinding;

if ((NULL != address) && strlen(address) > 0)

{

webServiceBinding.endpoint = address;

}

soap_set_mode(webServiceBinding.soap, SOAP_C_UTFSTRING);

userDetailInfo->setRetCode(RET_CODE_ERROR_UNKNOWN);

UserDetailInfoWS__getUserInfoByUserId request;

std::string id = userDetailInfo->getUserId();

request.userId = &id;

UserDetailInfoWS__getUserInfoByUserIdResponse response;

int ret = webServiceBinding.__UserDetailInfoWS__getUserInfoByUserId(&request, &response);

if (SOAP_OK == ret)

{

if (NULL == response.result)

{

userDetailInfo->setRetCode(RET_CODE_NULL_OBJECT);

userDetailInfo->setErrorDesc("no return value");

return userDetailInfo->getRetCode();

}

userDetailInfo->setRetCode(response.result->retCode);

if (RET_CODE_SUCCESS != userDetailInfo->getRetCode())

{

userDetailInfo->setErrorDesc(*response.result->desc);

}

else

{

if (NULL == response.result->userOperateInfo)

{

userDetailInfo->setRetCode(RET_CODE_NULL_OBJECT);

userDetailInfo->setErrorDesc("no return info");

return userDetailInfo->getRetCode();

}

userDetailInfo->setDescript(*response.result->userOperateInfo->descript);

userDetailInfo->setDepartmentId(*response.result->userOperateInfo->departmentId);

userDetailInfo->setEnabled(response.result->userOperateInfo->enable);

userDetailInfo->setLoginName(*response.result->userOperateInfo->loginName);

userDetailInfo->setPassword(*response.result->userOperateInfo->password);

userDetailInfo->setUserName(*response.result->userOperateInfo->name);

}

}

else

{

userDetailInfo->setRetCode(RET_CODE_SOAP_ERROR);

userDetailInfo->setDescript(*soap_faultstring(webServiceBinding.soap));

}

return userDetailInfo->getRetCode();

}

6.导入多个wsdl文件

[plain] view
plaincopy

wsdl2h.exe -sck -t e:\test\typemap.dat -o onvif.h analytics.wsdl analyticsdevice.wsdl deviceio.wsdl devicemgmt.wsdl display.wsdl event.wsdl imaging.wsdl media.wsdl ptz.wsdl Receiver.wsdl Recording.wsdl remotediscovery.wsdl Replay.wsdl Search.wsdl

7.操作参数:

以下就是wsdl2h的选项:
-o 文件名,指定输出头文件

-n 名空间前缀 代替默认的ns

-c 产生纯C代码,否则是C++代码

-s 不要使用STL代码

-t 文件名,指定type map文件,默认为typemap.dat

-e 禁止为enum成员加上名空间前缀

type map文件用于指定SOAP/XML中的类型与C/C++之间的转换规则,比如在wsmap.dat里写

xsd__string = | std::wstring | wchar_t*

那么SOAP/XML中的string将转换成std::wstring或wchar_t*,这样能更好地支持中文。

接着就是讲.h文件生成.cpp文件

soapcpp2.exe接的选项如下

-C 仅生成客户端代码

-S 仅生成服务器端代码

-L 不要产生soapClientLib.c和soapServerLib.c文件

-c 产生纯C代码,否则是C++代码(与头文件有关)

-I 指定imp<wbr>ort路径(见上文) </wbr>

-x 不要产生XML示例文件

-i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。

这里一般需要带上-x,不然会生成一大堆的xml文件。

-i选项也要带上,不然不会生成soapXXXXBindingService.cpp和soapXXXXBindingService.h文件

-I 选项一般也要带上,-I 后接gsoap路径中import目录

一般是-I E:\workspace\onvif\gsoap-2.8\gsoap\import;E:\workspace\onvif\gsoap-2.8\gsoap这样的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: