见:http://blog.csdn.net/sunsun314/article/details/52735664
通过IDE自动生成的代码调用webservice服务
我们的IDE一般来说都是能够通过各种各样的工具来支持我们的开发使我们的开发变得更加的便捷。对于webservice来说自然也是如此,所以我们可以轻松的调用webservice插件来进行客户端代码的生成。在这里我们直接new就可以了

把我们本地的WSDL的地址输入进去

直接finish,自动生成了一堆的类和对象

然后我们直接根据以下的方法就可以测试我们的webservice接口的情况了
- package?localhost.webserviceTest.test;??
- ??
- import?org.apache.axis.AxisFault;??
- import?localhost.webserviceTest.services.webServiceTest.HellowWorldProxy;??
- import?localhost.webserviceTest.services.webServiceTest.WebServiceTestSoapBindingStub;??
- ??
- public?class?webClientTest?{??
- ??????
- ????static?void?main(String[]?args?){??
- ????????try?{??
- ????????????HellowWorldProxy?pHellowWorldProxy=?new?HellowWorldProxy();??
- ????????????System.out.print(pHellowWorldProxy.test("?zhengfang.sun"));??
- ??????????
- ????????}?catch?(Exception?e)?{??
- ??????????????
- ????????????e.printStackTrace();??
- ????????}??
- ??????????
- ????}??
- }??
结果如下图所示

org.apache.axis.client.Call调用
Webservice接口我们自然可以通过IDE提供的功能进行生成,但是有时候也是会出现意外的,提供webservice的系统或者是服务方所在的网络环境和调用端的不连通,没有办法通过IDE进行生成,这个时候掌握其本质的调用方法就变得尤为重要
这种方法适用于在能够看到WSDL或者是能够知晓服务方提供的服务信息的前提下就可以使用。其核心是通过org.apache.axis.client.Call这个类进行webservice的调用。
package?localhost.webserviceTest.test;????
- ????
- import?java.net.MalformedURLException;????
- import?java.rmi.RemoteException;????
- ???
- import?javax.xml.namespace.QName;????
- import?javax.xml.rpc.ServiceException;??
- ??
- import?org.apache.axis.client.Call;????
- import?org.apache.axis.client.Service;????
- class?ClientTest?{????
- ????
- ????void?main(String[]?args)?throws?MalformedURLException,?RemoteException?{????
- ????????????
- ????????Service?service?=?new?Service();????
- ????????try?{????
- ???????????????
- ???????????Call?call?=?(Call)?service.createCall();????
- ??????????????
- ????call.setTargetEndpointAddress(new?java.net.URL("http://localhost:8080/webserviceTest/services/webServiceTest?wsdl"));????
- ????????????
- ????call.setOperationName(new?QName("http://localhost:8080/webserviceTest/services/webServiceTest","test"));???
- ?????????????
- ?????????????
- ????call.addParameter("testStr",?org.apache.axis.Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);????
- //设置返回的类型????
- ????call.setReturnType(org.apache.axis.Constants.XSD_STRING);????
- ????????????????
- ???????????String?name?=?"zhengfang.sun1";???
- //执行,调用webservice????
- ???????????String?result?=?(String)?call.invoke(new?Object[]{name});????
- ???????????System.out.println(result);?????????????????
- catch?(ServiceException?e)?{????
- ???????????e.printStackTrace();????
- ????????}????
- ????}????
- }????
方法的执行结果