1. 程式人生 > >Linux 下使用Webservice gSOAP教程(二)

Linux 下使用Webservice gSOAP教程(二)

生成wsdl檔案步驟

標頭檔案如下

// ws_interface.h

#ifndef  WS_INTERFACE_H #define  WS_INTERFACE_H // 注意:以下注釋是必要的 //gsoap ns service name: ws_interface //gsoap ns service style: rpc //gsoap ns service namespace: https://127.0.0.1:4433/cgi-bin/ws_interface.wsdl //gsoap ns service location: https://127.0.0.1:4433/cgi-bin //gsoap ns service executable: ws_interface.cgi //gsoap ns service encoding: encoded //gsoap ns schema namespace: urn:ws_interface typedef struct ns__AddGroupRequest {     char *validationCode;     char *name;     char *desc; } ns__AddGroupRequest; typedef struct ns__AddGroupResponse {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__AddGroupResponse; typedef struct ns__DelGroupRequest {     char *validationCode;     char *name; } ns__DelGroupRequest; typedef struct ns__DelGroupResponse {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__DelGroupResponse; typedef struct ns__OpGroupRequest {     char *validationCode;     char *name;     int enable;     // 0 disable, 1 enable } ns__OpGroupRequest; typedef struct ns__OpGroupResponse {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__OpGroupResponse; typedef struct ns__ModifyGroupRequest {     char *validationCode;     char *name;     char *newname; } ns__ModifyGroupRequest; typedef struct ns__ModifyGroupResponse {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__ModifyGroupResponse; typedef struct ns__AddUserRequest {     char *validationCode;     char *name;     char *truename;     char *passwd;     char *groupname;            // user belong to group name } ns__AddUserRequest; typedef struct ns__AddUserResponse {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__AddUserResponse; typedef struct ns__DelUserRequest {     char *validationCode;     char *name; } ns__DelUserRequest; typedef struct ns__DelUserResponse {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__DelUserResponse; typedef struct ns__OpUserRequest {     char *validationCode;     char *name;     int enable;     // 0 disable, 1 enable } ns__OpUserRequest; typedef struct ns__OpUserResponse {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__OpUserResponse; typedef struct ns__ModifyUserRequest {     char *validationCode;     char *name;     char *newname;     char *truename;     char *passwd;     char *groupname;            // user belong to group name } ns__ModifyUserRequest; typedef struct ns__ModifyUserResponse {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__ModifyUserResponse; int ns__AddGroup( ns__AddGroupRequest *AddGroup, ns__AddGroupResponse *AddGroupResponse ); int ns__DelGroup( ns__DelGroupRequest *DelGroupRequest, ns__DelGroupResponse *DelGroupResponse ); int ns__OpGroup( ns__OpGroupRequest *OpGroupRequest, ns__OpGroupResponse *OpGroupResponse ); int ns__ModifyGroup( ns__ModifyGroupRequest *ModifyGroupRequest, ns__ModifyGroupResponse *ModifyGroupResponse ); int ns__AddUser( ns__AddUserRequest *AddUserRequest, ns__AddUserResponse *AddUserResponse ); int ns__DelUser( ns__DelUserRequest *DelUserRequest, ns__DelUserResponse *DelUserResponse ); int ns__OpUser( ns__OpUserRequest *OpUserRequest, ns__OpUserResponse *OpUserResponse ); int ns__ModifyUser( ns__ModifyUserRequest *ModifyUserRequest, ns__ModifyUserResponse *ModifyUserResponse ); #endif

執行如下命令生成wsdl與原始檔:

soapcpp2 -Scd ./ws_interface ./ws_interface.h

關於型別的定義

不能因為某個結構一致,而使用typedef定義別名,這樣在生成的wsdl中會有兩個同樣名字的引數,

而導致wsdl檔案出現語法錯誤

例如,如下的標頭檔案是錯誤的:

// ws_interface.h

#ifndef  WS_INTERFACE_H #define  WS_INTERFACE_H //gsoap ns service name: ws_interface //gsoap ns service style: rpc //gsoap ns service namespace: https://127.0.0.1:4433/cgi-bin/ws_interface.wsdl //gsoap ns service location: https://127.0.0.1:4433/cgi-bin //gsoap ns service executable: ws_interface.cgi //gsoap ns service encoding: encoded //gsoap ns schema namespace: urn:ws_interface typedef struct ns__CommonResponse

{     int returnFlag;     char *msg;              // message to descript the return_flag } ns__CommonResponse; typedef struct ns__AddGroupRequest {     char *validationCode;     char *name;     char *desc; } ns__AddGroupRequest; typedef struct ns__DelGroupRequest {     char *validationCode;     char *name; } ns__DelGroupRequest;typedef struct ns__CommonResponse ns__AddGroupResponse;
int ns__AddGroup( ns__AddGroupRequest *AddGroup, ns__AddGroupResponse *AddGroupResponse );typedef struct ns__CommonResponse ns__DelGroupResponse; int ns__DelGroup( ns__DelGroupRequest *DelGroupRequest, ns__DelGroupResponse *DelGroupResponse ); #endif

關於gsoap生成的返回值

自定義標頭檔案group.h

typedef struct ns__CommonResponse111111111 {     int returnFlag;     char *msg;              // message to descript the return_flag } ns__CommonResponse2222222222; typedef struct ns__AddGroupRequest {     char *validationCode;     char *name;     char *desc; } ns__AddGroupRequest; int ns__AddGroup( ns__AddGroupRequest *AddGroupRequest, ns__CommonResponse2222222222 *CommonResponse );

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

然後用如下命令生成.c檔案:

soapcpp2 -S -c group.h

程式執行時,返回的資料被CommonResponse2222222222包裹:

<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="urn:ws_account"><SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<ns:CommonResponse2222222222>

<returnFlag>-460</returnFlag>

<msg>group name is empty</msg>

</ns:CommonResponse2222222222>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

注意:soapcpp2 -S -c group.h命令生成的樣例res.xml中資料是被CommonResponse111111111包裹,與實際執行程式後生成的資料不符