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规范的文件了. 中国.Net俱乐部转载此文。让我们一起进步,共享人类技术资源。[www.chinaaspx.com]
|