`
yesjavame
  • 浏览: 698865 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Axis2/C 基础入门

 
阅读更多

Axis2/C是一个用C语言实现的Web Service引擎。

Axis2/C基于Axis2架构 ,支持SOAP1.1SOAP1.2 协议,并且支持RESTful 风格的Web Service。基于Axis2/C的Web Service可以同时暴露为SOAP和RESTful风格的服务。

最近研究了一下Axis2/C,这篇文章详述了如何利用Axis2/C发布一个Web Service,并通过SOAP访问这个Web Service。

一、Axis2/C的下载和安装
1、可以从Axis2/C的官方网站下载到最新的Axis2/C发布包:Axis2/C下载

2、解压axis2c-bin-1.6.0-win32.zip,并在系统的环境变量中添加AXIS2_HOME并指向Axis2/C的解压目录,这样可以确保后续编译过程中可以找到Axis2/C的依赖项;

3、在系统环境变量中找到Path项,编辑该项,在它的最开始加入C:\VS8\Common7\IDE,并以分号分隔,以此保证编译后的客户端程序可以顺利找到依赖项;

4、在Path的结尾添加%AXIS2_HOME%\lib,确保编译器可以找到Axis2/C的依赖项;

二、构建和发布服务
5、新建CPP文件,命名为:hello_svc.c,并加入如下代码:

  1  #include  " stdafx.h "
  2  
  3  #include  < axis2_svc_skeleton.h >
  4  #include  < axutil_log_default.h >
  5  #include  < axutil_error_default.h >
  6  #include  < axutil_array_list.h >
  7  #include  < axiom_text.h >
  8  #include  < axiom_node.h >
  9  #include  < axiom_element.h >
 10  #include  < stdio.h >
 11  
 12  axiom_node_t  * axis2_hello_greet( const  axutil_env_t  * env, axiom_node_t  * node);
 13  int  AXIS2_CALL hello_free(axis2_svc_skeleton_t  * svc_skeleton,  const  axutil_env_t  * env);
 14  axiom_node_t *  AXIS2_CALL hello_invoke(axis2_svc_skeleton_t  * svc_skeleton,  const  axutil_env_t  * env, axiom_node_t  * node, axis2_msg_ctx_t  * msg_ctx);
 15  int  AXIS2_CALL hello_init(axis2_svc_skeleton_t  * svc_skeleton,  const  axutil_env_t  * env);
 16  axiom_node_t *  AXIS2_CALL hello_on_fault(axis2_svc_skeleton_t  * svc_skeli,  const  axutil_env_t  * env, axiom_node_t  * node);
 17  axiom_node_t  * build_greeting_response( const  axutil_env_t  * env, axis2_char_t  * greeting);
 18  
 19  axiom_node_t  * axis2_hello_greet( const  axutil_env_t  * env, axiom_node_t  * node)
 20  {
 21      axiom_node_t  * client_greeting_node  =  NULL;
 22      axiom_node_t  * return_node  =  NULL;
 23      AXIS2_ENV_CHECK(env, NULL);
 24       if  (node)
 25      {
 26          client_greeting_node  =  axiom_node_get_first_child(node, env);
 27           if  (client_greeting_node  &&  axiom_node_get_node_type(client_greeting_node, env)  ==  AXIOM_TEXT)
 28          {
 29              axiom_text_t  * greeting  =  (axiom_text_t  * )axiom_node_get_data_element(client_greeting_node, env);
 30               if  (greeting  &&  axiom_text_get_value(greeting , env))
 31              {
 32                   const  axis2_char_t  * greeting_str  =  axiom_text_get_value(greeting, env);
 33                  printf( " Client greeted saying \ " % s\ "  \n " , greeting_str);
 34                  return_node  =  build_greeting_response(env,  " Hello Client! " );
 35              }
 36          }
 37      }
 38       else
 39      {
 40          AXIS2_ERROR_SET(env -> error, AXIS2_ERROR_SVC_SKEL_INVALID_XML_FORMAT_IN_REQUEST, AXIS2_FAILURE);
 41          printf( " ERROR: invalid XML in request\n " );
 42          return_node  =  build_greeting_response(env,  " Client! Who are you? " );
 43      }
 44       return  return_node;
 45  }
 46  
 47  
 48  
 49  axiom_node_t  * build_greeting_response( const  axutil_env_t  * env, axis2_char_t  * greeting)
 50  {
 51      axiom_node_t *  greeting_om_node  =  NULL;
 52      axiom_element_t  *  greeting_om_ele  =  NULL;
 53      greeting_om_ele  =  axiom_element_create(env, NULL,  " greetResponse " , NULL,  & greeting_om_node);
 54      axiom_element_set_text(greeting_om_ele, env, greeting, greeting_om_node);
 55       return  greeting_om_node;
 56  }
 57  
 58  static   const  axis2_svc_skeleton_ops_t hello_svc_skeleton_ops_var  =  {
 59      hello_init,
 60      hello_invoke,
 61      hello_on_fault,
 62      hello_free
 63  };
 64  
 65  axis2_svc_skeleton_t  * axis2_hello_create( const  axutil_env_t  * env)
 66  {
 67      axis2_svc_skeleton_t  * svc_skeleton  =  NULL;
 68      svc_skeleton  =  (axis2_svc_skeleton_t  * ) AXIS2_MALLOC(env -> allocator,  sizeof (axis2_svc_skeleton_t));
 69      svc_skeleton -> ops  =   & hello_svc_skeleton_ops_var;
 70      svc_skeleton -> func_array  =  NULL;
 71       return  svc_skeleton;
 72  }
 73  
 74  int  AXIS2_CALL hello_init(axis2_svc_skeleton_t  * svc_skeleton,  const  axutil_env_t  * env)
 75  {
 76      svc_skeleton -> func_array  =  axutil_array_list_create(env,  0 );
 77      axutil_array_list_add(svc_skeleton -> func_array, env,  " helloString " );
 78       return  AXIS2_SUCCESS;
 79  }
 80  
 81  
 82  
 83  axiom_node_t *  AXIS2_CALL hello_invoke(axis2_svc_skeleton_t  * svc_skeleton,  const  axutil_env_t  * env, axiom_node_t  * node, axis2_msg_ctx_t  * msg_ctx)
 84  {
 85       return  axis2_hello_greet(env, node);
 86  }
 87  
 88  axiom_node_t *  AXIS2_CALL hello_on_fault(axis2_svc_skeleton_t  * svc_skeli,  const  axutil_env_t  * env, axiom_node_t  * node)
 89  {
 90      axiom_node_t  * error_node  =  NULL;
 91      axiom_node_t *  text_node  =  NULL;
 92      axiom_element_t  * error_ele  =  NULL;
 93      error_ele  =  axiom_element_create(env, node,  " EchoServiceError " , NULL,
 94           & error_node);
 95      axiom_element_set_text(error_ele, env,  " Echo service failed  " ,
 96          text_node);
 97       return  error_node;
 98  }
 99  
100  
101  
102  int  AXIS2_CALL hello_free(axis2_svc_skeleton_t  * svc_skeleton,
103              const  axutil_env_t  * env)
104  {
105       if  (svc_skeleton -> func_array)
106      {
107          axutil_array_list_free(svc_skeleton -> func_array, env);
108          svc_skeleton -> func_array  =  NULL;
109      }
110  
111       if  (svc_skeleton)
112      {
113          AXIS2_FREE(env -> allocator, svc_skeleton);
114          svc_skeleton  =  NULL;
115      }
116       return  AXIS2_SUCCESS;
117  }
118  
119  AXIS2_EXPORT  int  axis2_get_instance(axis2_svc_skeleton_t  ** inst,  const  axutil_env_t  * env)
120  {
121       * inst  =  axis2_hello_create(env);
122       if  ( ! ( * inst))
123      {
124           return  AXIS2_FAILURE;
125      }
126       return  AXIS2_SUCCESS;
127  }
128  
129  
130  
131  AXIS2_EXPORT  int  axis2_remove_instance(axis2_svc_skeleton_t  * inst,  const  axutil_env_t  * env)
132  {
133      axis2_status_t status  =  AXIS2_FAILURE;
134       if  (inst)
135      {
136          status  =  AXIS2_SVC_SKELETON_FREE(inst, env);
137      }
138       return  status;
139  }
140  


6、编译上述代码,在命令行运行如下编译命令:

$ C:\VS8\VC\bin\cl.exe  / " WIN32 "   / " _WINDOWS "   / " _MBCS "   / " AXIS2_DECLARE_EXPORT "   / " AXIS2_SVR_MULTI_THREADED "   / / nologo  / % AXIS2_HOME% \include  / I C:\\VS8\\VC\\include  / c hello_svc.c


7、链接生成的中间对象,并产生DLL,运行命令如下:

$ C:\VS8\VC\bin\link.exe  / nologo  / LIBPATH: % AXIS2_HOME% \lib  / LIBPATH:C:\\VS8\\VC\\lib axutil.lib axiom.lib axis2_parser.lib axis2_engine.lib  / DLL  / OUT:hello.dll  * .obj


8、在 % AXIS2_HOME%\services下创建目录hello, 将步骤7产生的hello.dll拷贝到其中,并创建服务描述文件services.xml,并插入如下内容:

 1  <? xml version="1.0" encoding="utf-8" ?>
 2  < service  name ="hello" >
 3     < parameter  name ="ServiceClass"  locked ="xsd:false" > hello </ parameter >
 4     < description >
 5      Quick start guide hello service sample.
 6     </ description >
 7     < operation  name ="greet" >
 8       < parameter  name ="wsamapping" > http://ws.apache.org/axis2/c/samples/hello </ parameter >
 9     </ operation >
10  </ service >


9、启动Axis2服务器,执行如下命令:

$ axis2_http_server


三、访问服务
10、在VS中创建项目Axis2Test,并在 Axis2Test.cpp中插入这些代码:

  1  #include  " stdafx.h "
  2  
  3  #include  < stdio.h >
  4  #include  < axiom.h >
  5  #include  < axis2_util.h >
  6  #include  < axiom_soap.h >
  7  #include  < axis2_client.h >
  8  
  9  axiom_node_t  * build_om_request( const  axutil_env_t  * env);
 10  const  axis2_char_t  * process_om_response( const  axutil_env_t  * env, axiom_node_t  * node);
 11  
 12  int  _tmain( int  argc,  char **  argv)
 13  {
 14       const  axutil_env_t  * env  =  NULL;
 15       const  axis2_char_t  * address  =  NULL;
 16      axis2_endpoint_ref_t *  endpoint_ref  =  NULL;
 17      axis2_options_t  * options  =  NULL;
 18       const  axis2_char_t  * client_home  =  NULL;
 19      axis2_svc_client_t *  svc_client  =  NULL;
 20      axiom_node_t  * payload  =  NULL;
 21      axiom_node_t  * ret_node  =  NULL;
 22  
 23      env  =  axutil_env_create_all( " hello_client.log " , AXIS2_LOG_LEVEL_TRACE);
 24      options  =  axis2_options_create(env);
 25      address  =   " http://localhost:9090/axis2/services/hello " ;
 26  
 27       if  (argc  >   1 )
 28          address  =  argv[ 1 ];
 29  
 30       if  (axutil_strcmp(address,  " -h " ==   0 )
 31      {
 32          printf( " Usage : %s [endpoint_url]\n " , argv[ 0 ]);
 33          printf( " use -h for help\n " );
 34           return   0 ;
 35      }
 36  
 37      printf( " Using endpoint : %s\n " , address);
 38      endpoint_ref  =  axis2_endpoint_ref_create(env, address);
 39      axis2_options_set_to(options, env, endpoint_ref);
 40      axis2_options_set_action(options, env,  " http://ws.apache.org/axis2/c/samples/hello " );
 41  
 42      client_home  =   " E:\\mengli\\axis2c-bin-1.6.0-win32 " ; // AXIS2_GETENV("AXIS2C_HOME");
 43       if  ( ! client_home  &&   ! strcmp(client_home,  "" ))
 44          client_home  =   " ../.. " ;
 45  
 46      svc_client  =  axis2_svc_client_create(env, client_home);
 47       if  ( ! svc_client)
 48      {
 49          printf( " Error creating service client\n " );
 50          AXIS2_LOG_ERROR(env -> log, AXIS2_LOG_SI,  " Stub invoke FAILED: Error code: "
 51               "  %d :: %s " , env -> error -> error_number,
 52              AXIS2_ERROR_GET_MESSAGE(env -> error));
 53           return   - 1 ;
 54      }
 55  
 56      axis2_svc_client_set_options(svc_client, env, options);
 57      payload  =  build_om_request(env);
 58      ret_node  =  axis2_svc_client_send_receive(svc_client, env, payload);
 59  
 60       if  (ret_node)
 61      {
 62           const  axis2_char_t  * greeting  =  process_om_response(env, ret_node);
 63           if  (greeting)
 64              printf( " \nReceived greeting: \ " % s\ "  from service\n " , greeting);
 65  
 66          axiom_node_free_tree(ret_node, env);
 67          ret_node  =  NULL;
 68      }
 69       else
 70      {
 71          AXIS2_LOG_ERROR(env -> log, AXIS2_LOG_SI,  " Stub invoke FAILED: Error code: "
 72               "  %d :: %s " , env -> error -> error_number,
 73              AXIS2_ERROR_GET_MESSAGE(env -> error));
 74          printf( " hello client invoke FAILED!\n " );
 75      }
 76  
 77       if  (svc_client)
 78      {
 79          axis2_svc_client_free(svc_client, env);
 80          svc_client  =  NULL;
 81      }
 82  
 83       if  (env)
 84      {
 85          axutil_env_free((axutil_env_t  * ) env);
 86          env  =  NULL;
 87      }
 88  
 89       return   0 ;
 90  
 91  }
 92  
 93  axiom_node_t  *
 94  build_om_request( const  axutil_env_t  * env)
 95  {
 96      axiom_node_t *  greet_om_node  =  NULL;
 97      axiom_element_t  *  greet_om_ele  =  NULL;
 98  
 99      greet_om_ele  =  axiom_element_create(env, NULL,  " greet " , NULL,  & greet_om_node);
100      axiom_element_set_text(greet_om_ele, env,  " Hello Server! " , greet_om_node);
101  
102       return  greet_om_node;
103  }
104  
105  
106  
107  const  axis2_char_t  *
108  process_om_response( const  axutil_env_t  * env,
109                      axiom_node_t  * node)
110  {
111      axiom_node_t  * service_greeting_node  =  NULL;
112      axiom_node_t  * return_node  =  NULL;
113  
114       if  (node)
115      {
116          service_greeting_node  =  axiom_node_get_first_child(node, env);
117  
118           if  (service_greeting_node  &&
119              axiom_node_get_node_type(service_greeting_node, env)  ==  AXIOM_TEXT)
120          {
121              axiom_text_t  * greeting  =  (axiom_text_t  * )axiom_node_get_data_element(service_greeting_node, env);
122               if  (greeting  &&  axiom_text_get_value(greeting , env))
123              {
124                   return  axiom_text_get_value(greeting, env);
125              }
126          }
127      }
128  
129       return  NULL;
130  }
0
1
分享到:
评论

相关推荐

    axis2开发Web Services入门

    ### Axis2 开发 Web Services 入门 #### 知识点概述 本文旨在介绍如何使用 Axis2 开发 Web Services 的全过程,包括环境搭建、插件安装等基础准备工作,以及具体的开发流程与实例演示。 #### 1. 环境搭建 ##### ...

    Axis2-study.zip_axis2_doc

    描述中的"Axis2教程.doc"确认了这个文件包含了一个Axis2的入门教程,适合想要学习或了解Axis2基础的人群。 **Axis2基础知识** Axis2是基于Apache Axis1的一个全新设计和实现,它是用Java语言编写的,用于处理SOAP...

    Axis2_入门.doc

    最后,将ARR文件复制到Apache Tomcat服务器的Axis2部署目录下,通常是`C:\Tomcat5.5\webapps\axis2\WEB-INF\services`。一旦部署成功,我们可以通过HTTP请求来调用服务的方法。例如,可以访问`...

    Eclipse3.2+MyEclipse5.5M1+axis2_1.1.1+tomcat5.5 开发Web Services

    【开发Web服务的环境配置与步骤】 在开发Web服务时,使用Eclipse和相关的插件可以大大提高...通过理解上述步骤,开发者可以快速入门Web服务开发,并在此基础上深入学习Web服务的相关技术和标准,如SOAP、WSDL和UDDI。

    Packt.Publishing.Quickstart.Apache.Axis2.May.2008

    ### Apache Axis2 快速入门知识点详解 #### 一、Apache Axis2简介 Apache Axis2是基于Java的一个高性能、轻量级的Web服务框架。它不仅支持SOAP协议,还提供了RESTful风格的服务接口,旨在帮助开发者更高效地构建高...

    TipTec.Developing.Web.Services.with.Apache.CXF.and.Axis2.Jan.2010.rar

    标题中的“TipTec.Developing.Web.Services.with.Apache.CXF.and.Axis2.Jan.2010”表明这是一份关于使用Apache CXF和Axis2开发Web服务的教程资料,发布于2010年1月。Apache CXF和Axis2是两个流行的Java框架,用于...

    MyEclipse下开发Web Service(Axis)

    Tomcat启动验证**:通过浏览器访问`http://localhost:8080/axis2`,确认Axis Web服务正常运行。 #### 二、快速入门:SayHello示例 为了更好地理解如何使用Axis开发Web Service,我们从一个简单的SayHello实例开始...

    grbl4axis-master.zip

    固件的修改和优化通常需要一定的编程基础,特别是对C语言和Grbl协议的理解,但一旦设置成功,你就可以享受到四轴加工带来的便利。 值得注意的是,四轴CNC加工对于硬件的要求比三轴更高,需要更稳定的机械结构和精确...

    Python与AI之 入门

    Python与Java、C、C++并列为全球四大最流行的语言之一。与其他语言相比,Python更注重于让开发者关注于“做什么”,而非陷入“怎么做”的细节之中。这使得Python成为一种非常适合初学者入门的编程语言。 #### ...

    《python数据分析基础教程》.pdf

    例如,使用`plt.scatter()`函数可以绘制散点图,通过设置`s`参数控制点的大小,`c`参数设置颜色。`plt.show()`显示图形。 5. **数据分析——sklearn库**: scikit-learn(简称sklearn)是机器学习的重要库,提供...

    matlab入门基础

    ### MATLAB入门基础知识详解 #### 一、MATLAB简介与历史沿革 MATLAB是一个由美国Mathworks公司开发的专业科学计算软件,它集成了强大的数值计算、数据可视化和算法开发等功能,广泛应用于科学研究、工程设计、教育...

    Web Service基础培训_入门篇.pdf

    根据给定的文件信息,以下是对“Web Service基础培训_入门篇”的详细解析与扩展: ### Web Service概览 Web Service是一种分布式计算技术,旨在通过轻量级且与厂商无关的通信协议,允许不同系统之间跨网络(如...

    Matlab基础知识0201.zip

    本资料主要针对Matlab的基础知识进行讲解,帮助初学者快速入门。 一、Matlab界面与基本操作 1. 工作空间(Workspace):在Matlab中,工作空间是存储变量的地方,你可以在这里查看和管理所有的变量。 2. 命令窗口...

    Pandas Shift函数的基础入门学习笔记

    ### Pandas `shift` 函数基础入门学习笔记 在数据分析领域,Pandas 是一个非常重要的库,它提供了灵活高效的数据结构以及数据分析工具。其中 `shift` 函数是处理时间序列数据时常用的一个功能,用于向前或向后移动...

    01-Numpy入门1

    c = np.ones([2, 3]) ``` Numpy数组的属性包括维度(`ndim`)、形状(`shape`)和元素数量(`size`)。这些属性可以帮助我们了解数组的基本结构。例如: ```python d = np.diag(a) # 斜对角矩阵 print(d.ndim, d....

    c游戏编程

    总之,"简单的C游戏编程入门"涉及到C语言基础、算法与数据结构、游戏循环、图形库使用、碰撞检测以及动画实现等多个方面。通过学习和实践,你可以逐步掌握游戏开发的基本技能,为未来的项目打下坚实的基础。

    R语言入门教程.docx R语言是一个不断发展的工具,你可以通过持续学习和实践来不断提升自己 参与数据分析和统计建模的项目,

    创建矩阵matrix_data (c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3) ``` ### 数据框(Data Frame) 数据框是R中最常用的数据结构,类似于表格。使用以下代码创建数据框: ```R 创建数据框data_frame (Name = c(...

    Python精讲Numpy基础,大牛笔记详细解释.pdf

    本文将对 Numpy 的基础知识点进行总结,帮助大家快速入门 Numpy。 一、Ndarray 对象 Ndarray 对象是 Numpy 中的基本数据结构,它是一个多维数组,可以用于存储和操作大规模数据。Ndarray 对象可以通过列表、元组或...

    MATLAB-帮你快速入门MATLAB(绘图篇).pdf

    ### MATLAB绘图基础知识点 #### 一、MATLAB概述 MATLAB是一款强大的数值计算软件,广泛应用于科学研究、工程计算、数据分析等领域。它不仅擅长矩阵运算,还提供了丰富的绘图功能,便于用户直观地展示数据。 #### ...

Global site tag (gtag.js) - Google Analytics