Axis入门(3)

WEB services客户端例子

说明:本文材料取自Axis user's guide
在Apache的公共Axis服务器上有一个echoString的服务,你向该服务发一个串,该服务能够返回同一个串给你.下面我们写一个WEB services客户端例子来调用该服务:
1 import org.apache.axis.client.Call;
2 import org.apache.axis.client.Service;
3 import javax.xml.namespace.QName;
4
5 public class TestClient {
6 public static void main(String [] args) {
7 try {
8 String endpoint =
9 " http://nagoya.apache.org:5049/axis/services/echo ";
10
//创建两个JAX-RPC对象
11 Service service = new Service();
12 Call call = (Call) service.createCall();
13
//SOAP消息的目的地
14 call.setTargetEndpointAddress( new java.net.URL(endpoint) );
//要调用的操作名称
15 call.setOperationName(new QName(" http://soapinterop.org/ ", "echoString"));
16
//用数组形式传入参数
17 String ret = (String) call.invoke( new Object[] { "Hello!" } );
18
19 System.out.println("Sent 'Hello!', got '" + ret + "'");
20 } catch (Exception e) {
21 System.err.println(e.toString());
22 }
23 }
24 }
上面的文件在axis的D:\axis\axis-1_1\samples\userguide\example1目录下.

加入需要的包,连接上网络,运行程序,你将得到下面的结果:
Sent 'Hello!', got 'Hello!'

用tcpmon或SOAP monitor工具查看传递的SOAP消息,如下:

1<soap-env:envelope xmlns:soap-env=" http://schemas.xmlsoap.org/soap/envelope/ " xmlns:xsd=" http://www.w3.org/2001/XMLSchema " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">
2<soap-env:body>
3<ns1:echostring xmlns:ns1=" http://soapinterop.org/ ">
4<arg0 xsi:type="xsd:string">Hello!</arg0>
5</ns1:echostring>
6</soap-env:body>
7</soap-env:envelope>

这个就是一个符合SOAP规范的文件了.

Published At
Categories with Web编程
Tagged with
comments powered by Disqus