Axis入门(3)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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消息,如下:

<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<SOAP-ENV:Body>

<ns1:echoString xmlns:ns1="http://soapinterop.org/">

<arg0 xsi:type="xsd:string">Hello!</arg0>

</ns1:echoString>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航