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;