使用gSoap开发Web Service C/C++客户端[转]

[email protected]

2006-08-02

一、开发环境准备

  1、从gSoap官方网站 http://gsoap2.sourceforge.net 下载软件

  2、根据文档安装配置gSoap()

二、如何开发WEB服务客户端

  1、基本流程

    (1)WEB服务提供者获取WEB ServiceWSDL文件;

    (2)使用gSoap工具wsdl2h,根据WSDL文件生成C/C++语法结构的头文件;

    (3)使用gSoap预编译器soapcpp2,根据.h头文件中定义的信息来生成客户端代码框架;

    (4)实现客户端例程;

    (5)GCC编译客户端,生成可执行代码。

  2、一个例子

    (1)WEB服务Hello.jws ( http://castor.ihep.ac.cn:8080/axis/Hello.jws ):

       public class Hello {

        public String hello(String name) {

               if(name==null)

                       name = "";

               return name +", welcome to the world of web service!";

         }

       }

    (2)获取WEB服务描述文件hello.wdsl

       http://castor.ihep.ac.cn:8080/axis/Hello.jws?wsdl

    (3)使用gSoap工具wsdl2h,根据WSDL文件生成C/C++语法结构的头文件hello.h

       wsdl2h -c hello.wsdl        

       -c 表示生成纯C头文件,不加-c生成C++头文件

    (4)使用gSoap预编译器soapcpp2,根据.h头文件中定义的信息来生成客户端代码框架

       soapcpp2 -c hello.h

       -c 表示生成纯C头代码,不加-c生成C++代码

    (5)实现客户端例程hello.c

       #include "soapH.h"

       #include "HelloSoapBinding.nsmap"

       int main()

       {

          struct soap soap;

          struct ns1__helloResponse ret;

          soap_init(&soap);

          if (soap_call_ns1__hello(&soap, NULL, NULL, "Aigui.LIU", &ret) == SOAP_OK)

               printf("%s/n", ret._helloReturn);

          else

             soap_print_fault(&soap, stderr);

          soap_destroy(&soap);

          soap_end(&soap);

          soap_done(&soap);

          return 0;

       }

    (6)GCC编译客户端,生成可执行代码hello

       $(CC) $(CFLAGS) -o hello hello.c soapC.c soapClient.c $(SOAPC)

       其中(Makefile中定义)

       GSOAP_PATH=/home/liuag/software/gsoap/gsoap-linux-2.6

       SOAPC=${GSOAP_PATH}/stdsoap2.c

       CC=gcc

       COFLAGS=-O2

       CWFLAGS=-Wall

       CIFLAGS=-I ${GSOAP_PATH}

       CMFLAGS=

       CFLAGS= $(CWFLAGS) $(COFLAGS) $(CIFLAGS) $(CMFLAGS)

    (7)运行结果

       $  ./hello

       $  Aigui.LIU, welcome to the world of web service!

三、参考文献

  1http://gsoap2.sourceforge.net

  2gSoap User Guide


转于

http://hi.baidu.com/songfeng163/blog/item/ce6617015239af0c7bec2cfc.html

你可能感兴趣的:(使用gSoap开发Web Service C/C++客户端[转])