public static String callService(String method , String params ,String paramsName){
String url = "xxx.asmx";// 提供接口的位址
String soapaction = "http://tempuri.org/"; //這個由接口提供者提供
Service service = new Service();
String result = "";
try{
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(soapaction, method)); // 設定要調用哪個方法
call.addParameter(new QName(soapaction, paramsName), // 設定要傳遞的參數--要和接口方提供的參數名一緻
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(new QName(soapaction, method), String.class); // 要傳回的資料類型
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction + method);
result = (String) call.invoke(new Object[] { params });// 調用方法并傳遞參數
System.out.print("接口傳回資訊:"+result);
}catch (Exception ex){
ex.printStackTrace();
}
return result;
}
備注:method 接口方法
params 接口參數
paramsName 接口參數名
public static String callService1(String method , String params ,String paramsName){String result = null;try { String endpoint = "http://122.227.252.10:5555/U9Interface/OAService.asmx?wsdl"; //直接引用遠端的wsdl檔案 //以下都是套路 Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setSOAPActionURI("http://tempuri.org/"+method); call.setOperationName(new QName("http://tempuri.org",method));//WSDL裡面描述的接口名稱 call.addParameter(new QName("http://tempuri.org",paramsName), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);//接口的參數 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設定傳回類型 call.setUseSOAPAction(true); result = (String) call.invoke(new Object[]{params});//給方法傳遞參數,并且調用方法 System.out.println("測試接口:"+result); } catch (Exception e) { System.err.println(e.toString()); }return result;