public static void main(String[] args){
// 参数列表
String param1 = "xxx";
String param2 = "xxx";
.....
Object[] o = {param1 , param2 ,....};
callWebserviceASMX("SendSms",o);
}
public static void callWebserviceASMX(String method, Object[] o) {
//获取webservice接口地址
String url = "xxxx.asmx";
//获取域名地址,server定义的
String soapaction = "xxx";
Service service = new Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
//设置要调用的方法
call.setOperationName(new QName(soapaction,method));
// 设置要传递的参数
call.addParameter(new QName(soapaction, "param1"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "param2"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
//设置要返回的数据类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction+method);
//调用方法并传递参数
String result = (String) call.invoke(o);
System.out.println("result is:::"+result);
} catch (Exception e) {
e.printStackTrace();
}
}
中途报了几个缺少jar包的错,看了下别人的博客,然后引入一下就行了
上面这个方法调用成功后,我作为接口用dubbo+zookeeper发布到一个公司的接口管理平台,结果其他端调用这个接口时,Service创建不了,找不Service这个类,据说是包没导,然后导了也没用,于是我又换了另外一种方式,
public void sendMsg(Map map) throws Exception{
String phoneStr = (String) map.get("phoneStr");
String msg = (String) map.get("msg");
String sendTime = (String) map.get("sendTime");
Object[] o = {phoneStr,msg,sendTime};
String url = Configuration.getInstance().getValue("xxx");
String ECID = Configuration.getInstance().getValue("xxx");
String UserCode = Configuration.getInstance().getValue("xxx");
String UserPass = Configuration.getInstance().getValue("xxx");
Object[] o1 = {ECID,UserCode,UserPass};
Object[] params = new Object[o.length+o1.length];
//这里是因为有一些配置是直接在properties文件里写的然后参数顺序也固定,所以我就通过复制数组的方式,把传过来的参数和配置放一起了
System.arraycopy(o1,0,params,0,o1.length);
System.arraycopy(o,0,params,o1.length,o.length);
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
String webServicePath = url;
Client client = null;
client = dcf.createClient(webServicePath);
client.invoke("SendSms",params);
}
这个方式在其他端就可以正常调用了
网友评论