一、服务端:
1.编写头文件,简单举例如下:
//gsoap ns service name: test
//gsoap ns service protocol: SOAP
//gsoap ns service style: rpc
//gsoap ns service namespace: http://172.16.47.87:8080/test/
//gsoap ns service location: http://172.16.47.87:8080/test/
//gsoap ns schema namespace: urn:nszfpt
int ns__add(double a, double b ,double & result);
其中:
Service name为类名;
ns为函数声明前缀,函数声明必须最后一个参数为指针或引用,即返回结果。
这些信息会生成.nsmap文件。
函数的定义类似:int ns__add(soap* ,double a, double b,double & result);
2.生成框架文件:soapcpp2 -S test.h
3.编写main函数:
Stand-Alone服务端main函数如下:
#include "soapStub.h"
#include "test.nsmap"
int main()
{
struct soap soap;
int m, s; // master and slave sockets
soap_init(&soap);
m = soap_bind(&soap, "172.16.47.87", 8080, 100);
if (m < 0)
soap_print_fault(&soap, stderr);
else
{
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for (int i = 1; ; i++)
{
s = soap_accept(&soap);
if (s < 0)
{
soap_print_fault(&soap, stderr);
break;
}
fprintf(stderr, "%d: accepted connection from IP=%d.%d.%d.%d socket=%d", i,
(soap.ip >> 24)&0xFF, (soap.ip >> 16)&0xFF, (soap.ip >> 8)&0xFF, soap.ip&0xFF, s);
if (soap_serve(&soap) != SOAP_OK) // process RPC request
soap_print_fault(&soap, stderr); // print error
fprintf(stderr, "request served\n");
soap_destroy(&soap); // clean up class instances
soap_end(&soap); // clean up everything and close socket
}
}
soap_done(&soap); // close master socket and detach context
}
3.修改cmakelist.txt文件:
add_executable(test test.cpp stdsoap2.cpp soapServer.cpp soapC.cpp main.cpp)
二、客户端代码:
1. 从wsdl文件生成头文件:wsdl2h -o outfile.h infile.wsdl
2. 从头文件生成代理类:soapcpp2 -I/usr/local/share/gsoap/import/ -i -C test.h
3. 调用web服务:
testProxy test(SOAP_XML_INDENT);
double result;
std::cout<<test.add(1,1,result)<<std::endl;
std::cout<<result<<std::endl;
4. 修改cmakelist.txt文件:
add_executable(client main.cpp soapC.cpp soaptestProxy.cpp stdsoap2.cpp)