我的测试环境是在Ubuntu12.04,windows下基本一样。
1.准备wsdl文件
下面是我用来测试的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>
2.生成first.h
命令行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
);
3.生成实现代码
命令行soapcpp2 -i first.h
我需要的是C++的源文件,所以使用选项-i,生成纯C源文件,使用选项-c。
4.编写服务器端实现
源文件 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;
}
5.编写客户端实现
源文件 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;
}
6.编译
将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++
7.测试
服务端有两种工作方式,一种是作为cgi运行,需要http服务器支持;另一种是作为单独的服务器端运行。如果是第一种方式,上面的firstClient.cpp就需要修改server,指向你的cgi链接;第二种方式,直接在命令行执行./firstServer 2000,参数是端口,以2000为例。
执行客户端测试:
./firstClient add 1 1
result = 2
看到结果了,打完收工。
分享到:
相关推荐
gsoap程序
使用gsoap和c++开发的webservice客户端和服务端程序,
gSOAP 开发 Web Service 程序
gsoap的测试(TestWebService)
用gSOAP开发WebService程序
Qt下gsoap访问webService方法程序。
通过交叉编译将gsoap移植到安卓平台,开发gsoap服务端程序以及用于测试的客户端程序
C开发webService客户端代码实现,服务器端用NET实现,客户端主要用在嵌入式平台上。
gsoap做的"天气查询客户端", Makefile中讲解了利用gsoap开发webservice的一切步骤。对于初学者如何使用网上提供的webservice描述文件(*.wsdl)进行webservice开发是一个很好的示例。
C++ 通过gsoap生成WebServer 和WebClient 内附gsoap包和测试源码,内部附有gsoap包、简单的使用说明以及测试代码
gSOAP编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现,从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多。绝大多数的C++web服务工具包提供一组API函数类库来处理特定的SOAP数据结构,这样就使得用户...
gsoap,直接下载,调用函数即可,实现通讯
gsoap
qt程序通过gsoap访问webservice示例程序代码!
gSOAP 编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现,从而让C/C++语言开发web 服务或客户端程序的工作变得轻松了很多。绝大多数的C++web 服务工具包提供一组API 函数类库来处理特定的SOAP 数据结构,这样就使得...
gsoap批处理文件生成工具,使用批处理实现生成相关C++代码文件。 使用前先下载gsoap源程序
gSOAP编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现,从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多。绝大多数的C++web服务工具包提供一组API函数类库来处理特定的SOAP数据结构,这样就使得用户...
gSOAP 编译工具提供了一个SOAP关于C/C++ 语言的实现,从而让C/C++语言开发Web Services服务端或客户端程序的工作变得轻松了很多。甚至,即使你对Web Services不甚了解都没有关系,有了gSOAP这样的工具,你也能开发...
gsoap最新版本最新版本,C/C++调用Web Service需要用到soap库,一般使用的有gsoap和axis cpp两种实现,这里使用gsoap来调用。gsoap是sourceforge上的一个开源项目,目前版本是2.7.6c,使用简单,可以在 linxu、...
介绍关于gsoap的使用方法及相关概念,内容很详细