gsoap生成webservice调用客户端接口

来源:互联网 发布:淘宝网上买仓鼠 编辑:程序博客网 时间:2024/05/20 04:09

http://blog.csdn.net/johnnywww/article/details/8187398


1.下载gsoap2.8

2.运行

[plain] view plaincopy
  1. wsdl2h.exe -o XXX.h XXX.wsdl  
wsdl文件可以是本地文件,也可以是服务器的wsdl,比如http://192.168.0.122:3333/ws/uss?wsdl

3.生成客户端代码

[plain] view plaincopy
  1. soapcpp2.exe  -L -x -C XXX.h -I .\gsoap-2.8\gsoap\import  

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

[plain] view plaincopy
  1. wsdl2h -nns别名 -N服务命名空间 -o XXX.h XXX.wsdl  
  2. wsdl2h -nuseraddns -NUserAddWS -oUserAddWebService.h userAddWebService.wsdl  
  3. wsdl2h -nuserloginns -NUserLoginWS -oUserLoginWebService.h userLoginWebService.wsdl  


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

[plain] view plaincopy
  1. soap2cpp -LCx -pCMSWS All.h -I ../../import  

拷贝gsoap2.8目录下的stdsoap2.h,stdsoap2.cpp到程序目录,并修改stdsoap2.cpp文件,将其中一个.nsmap文件包含进去
[plain] view plaincopy
  1. #include "xxx.nsmap"  


5.传输中文

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

如果用qt的QString将转换

[cpp] view plaincopy
  1. std::string CUIUtils::convertQStrToUTF8Str(const QString& value) {  
  2.     QByteArray arr =  value.toUtf8();  
  3.     return std::string(arr.data());  
  4. }  
  5.   
  6. QString CUIUtils::convertUTF8StrToQStr(const std::string& value) {  
  7.     return QString::fromUtf8(value.c_str());  
  8. }  

例子

[cpp] view plaincopy
  1. int CUserDetailInfoWebServiceImpl::getUserInfo(const char* address, CUserDetailInfo* userDetailInfo)  
  2. {  
  3.     UserDetailInfoWebServiceCXFImplServiceSoapBinding webServiceBinding;  
  4.     if ((NULL != address) && strlen(address) > 0)  
  5.     {  
  6.         webServiceBinding.endpoint = address;  
  7.     }  
  8.     soap_set_mode(webServiceBinding.soap, SOAP_C_UTFSTRING);  
  9.     userDetailInfo->setRetCode(RET_CODE_ERROR_UNKNOWN);  
  10.     UserDetailInfoWS__getUserInfoByUserId request;  
  11.     std::string id = userDetailInfo->getUserId();  
  12.     request.userId = &id;  
  13.     UserDetailInfoWS__getUserInfoByUserIdResponse response;  
  14.     int ret = webServiceBinding.__UserDetailInfoWS__getUserInfoByUserId(&request, &response);  
  15.     if (SOAP_OK == ret)  
  16.     {  
  17.         if (NULL == response.result)  
  18.         {  
  19.             userDetailInfo->setRetCode(RET_CODE_NULL_OBJECT);  
  20.             userDetailInfo->setErrorDesc("no return value");  
  21.             return userDetailInfo->getRetCode();  
  22.         }  
  23.         userDetailInfo->setRetCode(response.result->retCode);  
  24.         if (RET_CODE_SUCCESS != userDetailInfo->getRetCode())  
  25.         {  
  26.             userDetailInfo->setErrorDesc(*response.result->desc);  
  27.         }  
  28.         else  
  29.         {  
  30.             if (NULL == response.result->userOperateInfo)  
  31.             {  
  32.                 userDetailInfo->setRetCode(RET_CODE_NULL_OBJECT);  
  33.                 userDetailInfo->setErrorDesc("no return info");  
  34.                 return userDetailInfo->getRetCode();  
  35.             }  
  36.             userDetailInfo->setDescript(*response.result->userOperateInfo->descript);  
  37.             userDetailInfo->setDepartmentId(*response.result->userOperateInfo->departmentId);  
  38.             userDetailInfo->setEnabled(response.result->userOperateInfo->enable);  
  39.             userDetailInfo->setLoginName(*response.result->userOperateInfo->loginName);  
  40.             userDetailInfo->setPassword(*response.result->userOperateInfo->password);  
  41.             userDetailInfo->setUserName(*response.result->userOperateInfo->name);  
  42.         }  
  43.   
  44.     }  
  45.     else  
  46.     {  
  47.         userDetailInfo->setRetCode(RET_CODE_SOAP_ERROR);  
  48.         userDetailInfo->setDescript(*soap_faultstring(webServiceBinding.soap));  
  49.     }  
  50.     return userDetailInfo->getRetCode();  
  51.   
  52. }  

6.导入多个wsdl文件

[plain] view plaincopy
  1. 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这样的
原创粉丝点击