基于gsoap开发WebService服务返回结构体数组
发布时间:2020-12-16 22:37:30  所属栏目:安全  来源:网络整理 
            导读:基于gsoap开发WebService服务返回结构体数组 gsoap搭建和快速WebService示例编写,前面文章已经介绍过,此文直接讲关键点。 (1)返回的目标结构,开头以ns__,这与第一行//gsoap ns XXX 相关,例如 struct ns__EmployeeInfo{ int userid; char* name;}; (2
                
                
                
            |  
 基于gsoap开发WebService服务返回结构体数组 
  
 gsoap搭建和快速WebService示例编写,前面文章已经介绍过,此文直接讲关键点。 
  
 (1)返回的目标结构,开头以ns__,这与第一行//gsoap ns XXX 相关,例如 
 struct ns__EmployeeInfo
{
    int userid;
    char* name;
};struct ArrayOfEmp2
{struct ns__EmployeeInfo info;};接口函数:int ns__getEmp2(void *_,struct ArrayOfEmp2 &ccc); 
   这个写法你会发现,C#客户端更新wsdl结构后的返回值是ArrayOfEmp2,其内部有一个info属性则为返回的结构体内容。 
  
   下面是“处女座”的写法: 
  好处就是,在客户端返回值直接就是EmployeeInfo结构。 struct ns__ArrayOfEmp2
{struct ns__EmployeeInfo info;};
接口函数:int ns__getEmp2(void *_,struct ns__ArrayOfEmp2 &ccc); 
 (3)重点来了,返回结构体数组的写法: 
 struct ArrayOfEmp1  //此为正确写法
{struct ns__EmployeeInfo **__ptr;int __size;};接口函数:int ns__getAllEmp1(void *_,struct ArrayOfEmp1 &alal);几个注意点:父级结构名字不要带ns__;返回的目标结构成员的名字必须是__ptr,大小的名字必须是__size。 (4)接口实现代码 int ns__getAllEmp1(struct soap* soap,void *_,struct ArrayOfEmp1 &_return)
{
    _return.__size = 5;
    _return.__ptr = (struct ns__EmployeeInfo**)soap_malloc(soap,5*sizeof(struct ns__EmployeeInfo));
    _return.__ptr[0] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo));
    _return.__ptr[0]->userid=1000;
    _return.__ptr[0]->name= soap_strdup(soap,"name1");
    _return.__ptr[1] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo));
    _return.__ptr[1]->userid=1001;
    _return.__ptr[1]->name= soap_strdup(soap,"name2");
    _return.__ptr[2] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo));
    _return.__ptr[2]->userid=1002;
    //_return.__ptr[2]->name = soap_strdup(soap,"name3");
    _return.__ptr[2]->name = (char*)"name3";//这种形式的复制,也是没关系的
    _return.__ptr[3] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo));
    _return.__ptr[3]->userid = 1003;
    _return.__ptr[3]->name = (char*)"name4";
    _return.__ptr[4] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo));
    _return.__ptr[4]->userid = t.id;
    _return.__ptr[4]->name = soap_strdup(soap,t.name.c_str());
    return SOAP_OK;
}几个作死行为:  
 1、返回结构数组的父级结构体,写法上带ns__,如: 
 struct ns__ArrayOfEmp //此写法会导致返回的结构是ArrayOfEmp,且其Item属性为NULL
{struct ns__EmployeeInfo **__ptr;int __size;}; 
 ====以下无内容====   
 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
