天天看点

gsoap 创建 webservice

 gsoap 的用法

1. 首先编辑.h文件,模版如下:

注意:

service port
           

这里是我们webservice的地址。

#import "stlvector.h"

//gsoap ns1  service name:	sysinfoSOAP 
//gsoap ns1  service type:	sysinfo 
//gsoap ns1  service port:	http://127.0.0.1:10008 
//gsoap ns1  service namespace:	http://www.example.org/sysinfo/ 
//gsoap ns1  service transport:	http://schemas.xmlsoap.org/soap/http 



//gsoap ns1  service method-style:	fetch_USCOREcpu rpc
//gsoap ns1  service method-encoding:	fetch_USCOREcpu http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns1  service method-action:	fetch_USCOREcpu http://www.example.org/sysinfo/fetch_cpu
int ns1__fetch_USCOREcpu(
    std::string                        &result	///< Response parameter
);

//gsoap ns1  service method-style:	fetch_USCOREmemory rpc
//gsoap ns1  service method-encoding:	fetch_USCOREmemory http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns1  service method-action:	fetch_USCOREmemory http://www.example.org/sysinfo/fetch_cpu
int ns1__fetch_USCOREmemory(
    std::string                        &result	///< Response parameter
);

//gsoap ns1  service method-style:	fetch_USCOREio rpc
//gsoap ns1  service method-encoding:	fetch_USCOREio http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns1  service method-action:	fetch_USCOREio http://www.example.org/sysinfo/fetch_cpu
int ns1__fetch_USCOREio(
    std::string                        &result	///< Response parameter
);

//gsoap ns1  service method-style:	fetch_USCOREhd rpc
//gsoap ns1  service method-encoding:	fetch_USCOREhd http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns1  service method-action:	fetch_USCOREhd http://www.example.org/sysinfo/fetch_cpu
int ns1__fetch_USCOREhd(
    std::string                        &result	///< Response parameter
);

//gsoap ns1  service method-style:	fetch_USCOREprocess rpc
//gsoap ns1  service method-encoding:	fetch_USCOREprocess http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns1  service method-action:	fetch_USCOREprocess http://www.example.org/sysinfo/fetch_cpu
int ns1__fetch_USCOREprocess(
    std::string                        &result	///< Response parameter
);

//gsoap ns1  service method-style:	fetch_USCOREnetwork rpc
//gsoap ns1  service method-encoding:	fetch_USCOREnetwork http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns1  service method-action:	fetch_USCOREnetwork http://www.example.org/sysinfo/fetch_cpu
int ns1__fetch_USCOREnetwork(
    std::string                        &result	///< Response parameter
);

//gsoap ns1  service method-style:	set_USCOREnotify_USCOREparam rpc
//gsoap ns1  service method-encoding:	set_USCOREnotify_USCOREparam http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns1  service method-action:	set_USCOREnotify_USCOREparam http://www.example.org/sysinfo/fetch_cpu
int ns1__set_USCOREnotify_USCOREparam(
    std::string                         parameters,	///< Request parameter
    std::string                        &set_USCOREnotify_USCOREparamResponse	///< Response parameter
);

/* End of sysinfo.h */
           

2. 开始生成服务端代码

soap2cpp .h -I ( import path) -i

-i选项很重要,可以生成soapservice这样的类,也可以生成server的函数集合,试下就明白了。

copy到工程的文件不多,有

soapC.cpp

soapH.h

soapStub.h

soap[*]SOAPService.h/cpp

stdsoap2.h/cpp

sysinfoSOAP.nsmap

还有你的wsdl文件。

3. 提供客户端访问wsdl

客户端需要wsdl文件,webservice 的提供方式在stdsoap2.cpp中的http_get函数:

static int
http_get(struct soap *soap)
{ 
    FILE*fd = NULL;
    fd = fopen("sysinfo.wsdl", "rb"); //open WSDL file to copy
    if (!fd)
    {
        return 404; //return HTTP not found error
    }
    soap->http_content = "text/xml";  //HTTP header with text /xml content
    soap_response(soap,SOAP_FILE);
    for(;;)
    {
        size_t r = fread(soap->tmpbuf,1, sizeof(soap->tmpbuf), fd);
        if (!r)
        {
            break;
        }
        if (soap_send_raw(soap, soap->tmpbuf, r))
        {
            break; //cannot send, but little we can do about that
        }
    }
    fclose(fd);
    soap_end_send(soap);
    return SOAP_GET_METHOD;
}
           

4. 实现webservice函数

添加与server/service头文件中对应的函数实现即可。