java 调用 wsdl形式的webservice 示例

 1 import java.rmi.RemoteException;
 2 import javax.xml.rpc.ParameterMode;
 3 import javax.xml.rpc.ServiceException;
 4 import org.apache.axis.client.Call;
 5 import org.apache.axis.client.Service;
 6 import org.apache.axis.encoding.XMLType;
 7 
 8 public class webServiceTest {
 9     public String invokeRemoteFuc() {
10         String endpoint = "http://localhost:8080/webservice/services/helloworld";
11         String result = "no result!";
12         Service service = new Service();
13         Call call;
14         Object[] object = new Object[1];
15         object[0] = "Dear I miss you";//Object是用来存储方法的参数
16         try {
17             call = (Call) service.createCall();
18             call.setTargetEndpointAddress(endpoint);// 远程调用路径
19             call.setOperationName("say");// 调用的方法名
20 
21             // 设置参数名:
22             call.addParameter("str1", // 参数名
23                     XMLType.XSD_STRING,// 参数类型:String
24                     ParameterMode.IN);// 参数模式:'IN' or 'OUT'
25 
26             // 设置返回值类型:
27             call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String            
28 
29             result = (String) call.invoke(object);// 远程调用
30         } catch (ServiceException e) {
31             e.printStackTrace();
32         } catch (RemoteException e) {
33             e.printStackTrace();
34         }
35         return result;
36     }
37 
38     public static void main(String[] args) {
39         webServiceTest t = new webServiceTest();
40         String result = t.invokeRemoteFuc();
41         System.out.println(result);
42     }
43 }

来自:https://my.oschina.net/erichsbc/blog/148913

posted @ 2018-03-13 17:48  花儿为何那样红  阅读(1227)  评论(0编辑  收藏  举报