程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> Axis2/C基礎入門

Axis2/C基礎入門

編輯:關於C語言

Axis2/C是一個用C語言實現的Web Service引擎。

Axis2/C基於Axis2架構,支持SOAP1.1和SOAP1.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:VS8Common7IDE,並以分號分隔,以此保證編譯後的客戶端程序可以順利找到依賴項;

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" ", 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 ");
 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:VS8VCincl.exe /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "AXIS2_DECLARE_EXPORT" /D "AXIS2_SVR_MULTI_THREADED" /w /nologo /I  %AXIS2_HOME%include /I C:\VS8\VC\include /c hello_svc.c
7、鏈接生成的中間對象,並產生DLL,運行命令如下:
$ C:VS8VCinlink.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] ", argv[0]);
 33         printf("use -h for help ");
 34         return 0;
 35     }
 36
 37     printf("Using endpoint : %s ", 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 ");
 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(" Received greeting: "%s" from service ", 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! ");
 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 }
11、編譯並執行該程序,會向遠程服務發送字符串Hello Service!,並得到響應Hello Client!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved