下面是我用来测试的wsdl定义文件,定义了一个add方法,从官方的Sample中改来的。
<?xml version="1.0" encoding="UTF-8"?> <definitions name="first" targetNamespace="http://localhost/wsdl/first.wsdl" xmlns:tns="http://localhost/wsdl/first.wsdl" 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:first" xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/" xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <schema targetNamespace="urn:first" 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:first" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified"> <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/> </schema> </types> <message name="addRequest"> <part name="a" type="xsd:double"/> <part name="b" type="xsd:double"/> </message> <message name="addResponse"> <part name="result" type="xsd:double"/> </message> <portType name="firstPortType"> <operation name="add"> <documentation>Service definition of function ns__add</documentation> <input message="tns:addRequest"/> <output message="tns:addResponse"/> </operation> </portType> <binding name="first" type="tns:firstPortType"> <SOAP:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="add"> <SOAP:operation style="rpc" soapAction=""/> <input> <SOAP:body use="encoded" namespace="urn:first" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <SOAP:body use="encoded" namespace="urn:first" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <service name="first"> <documentation>my first service definition</documentation> <port name="first" binding="tns:first"> <SOAP:address location="http://websrv.cs.fsu.edu/~engelen/calcserver.cgi"/> </port> </service> </definitions>
命令行wsdl2h -s -o first.h first.wsdl
-s选项,指定不要使用STL。
这样就生成了first.h头文件,这个文件里声明了add这个方法,不过名字有点不一样:
int ns2__add(
double a, ///< Request parameter
double b, ///< Request parameter
double &result ///< Response parameter
);
命令行soapcpp2 -i first.h
我需要的是C++的源文件,所以使用选项-i,生成纯C源文件,使用选项-c。
源文件 firstServer.cpp
#include "soapfirstService.h" #include "first.nsmap" int main(int argc, char **argv) { firstService first; if (argc < 2) first.serve(); /* serve as CGI application */ else { int port = atoi(argv[1]); if (!port) { fprintf(stderr, "Usage: calcserver++ <port>\n"); exit(0); } /* run iterative server on port until fatal error */ if (first.run(port)) { first.soap_stream_fault(std::cerr); exit(-1); } } return 0; } int firstService::add(double a, double b, double& result) { result = a + b; return SOAP_OK; }
源文件 firstClient.cpp
#include "soapfirstProxy.h" #include "first.nsmap" const char server[] = "localhost:2000"; int main(int argc, char **argv) { if (argc < 4) { fprintf(stderr, "Usage: [add|sub|mul|div|pow] num num\n"); exit(0); } double a, b, result; a = strtod(argv[2], NULL); b = strtod(argv[3], NULL); firstProxy first; first.soap_endpoint = server; switch (*argv[1]) { case 'a': first.add(a, b, result); break; default: fprintf(stderr, "Unknown command\n"); exit(0); } if (first.error) first.soap_stream_fault(std::cerr); else printf("result = %g\n", result); return 0; }
将gsoap-xxx/gsoap目录下的stdsoap2.h 和stdsoap2.cpp拷贝到当前目录。
编译服务器端:
g++ -o firstServer firstClient.cpp stdsoap2.cpp soapC.cpp soapfirstService.cpp -lgsoap++
编译客户端:
g++ -o firstClient firstClient.cpp stdsoap2.cpp soapC.cpp soapfirstProxy.cpp -lgsoap++
服务端有两种工作方式,一种是作为cgi运行,需要http服务器支持;另一种是作为单独的服务器端运行。如果是第一种方式,上面的firstClient.cpp就需要修改server,指向你的cgi链接;第二种方式,直接在命令行执行./firstServer 2000,参数是端口,以2000为例。
执行客户端测试:
./firstClient add 1 1
result = 2
看到结果了,打完收工。