gsoap 创建 webservice

这篇博客介绍了如何利用gsoap工具创建Web服务。首先,需要编辑.h文件并使用soap2cpp命令生成必要的源代码文件,包括soapC.cpp、soapH.h等。接着,提到了客户端获取WSDL文件的方式,通过修改stdsoap2.cpp中的http_get函数来提供服务。最后,文章指出要实现服务头文件中对应的函数以完成服务功能。

 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头文件中对应的函数实现即可。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值