1.下载gsoap2.8
2.运行
wsdl2h.exe -o XXX.h XXX.wsdlwsdl文件可以是本地文件,也可以是服务器的wsdl,比如http://192.168.0.122:3333/ws/uss?wsdl
3.生成客户端代码
soapcpp2.exe -L -x -C XXX.h -I .\gsoap-2.8\gsoap\import
4.如果有多个服务,那么就将要使用命名空间
wsdl2h -nns别名 -N服务命名空间 -o XXX.h XXX.wsdl wsdl2h -nuseraddns -NUserAddWS -oUserAddWebService.h userAddWebService.wsdl wsdl2h -nuserloginns -NUserLoginWS -oUserLoginWebService.h userLoginWebService.wsdl
soap2cpp -LCx -pCMSWS All.h -I ../../import
#include "xxx.nsmap"
5.传输中文
调用方法转为utf-8传输soap_set_mode( soap*, SOAP_C_UTFSTRING )
如果用qt的QString将转换
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()); }
例子
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文件
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 文件名,指定输出头文件