在浏览器上访问服务器端的服务,可以下载到WSDL文件,通过Axis的相关工具,可以自动从WSDL文件中生成Web Service的客户端代码。 编写一个WSDL2Java.bat文件,其内容如下: set Axis_Lib=.\lib set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% set Output_Path=.\src set Package=server.com set wsdl_path=http://localhost:8080/AxisTest/services/ SayHello?wsdl %Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% %wsdl_path%
执行这个批处理文件就可以生成client stub. 生成的stub client文件列表为:SayHello.java,SayHelloService.java,SayHelloServiceLocator.java,SayHelloSoapBindingStub.java . 9、编写客户端程序,编译并执行 1)、Stubs方式 下面是一段junit测试客户端代码。 import java.net.URL; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class TestWSClient extends TestCase { public TestWSClient(String string) { super(string); } public void SayHelloClient() throws Exception { SayHelloService service = new SayHelloServiceLocator(); SayHello_PortType client = service.getSayHello() ; String retValue = client.getName("clientname"); System.out.println(retValue); } public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new TestWSClient("SayHelloClient")); return suite; } } 2)、动态调用方式: try { // Options options = new Options(args); String endpointURL = "http://localhost:8080/AxisTest/services/SayHello"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress( new java.net.URL(endpointURL) ); call.setOperationName( new QName("SayHello", "getName") ); String res = (String) call.invoke( new Object[] {"Jack"} ); System.out.println( res ); } catch (Exception&nb上一页 [1] [2] [3] [4] 下一页
|
|