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頭檔案中對應的函數實作即可。