碰到了一个WebService.asmx的接口,在网上搜索了一下解决方式,记录一下,方便后续使用
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Test {
public static void main(String[] args) {
try {
Object[] params = new Object[]{"key01","001","Inv",“”};
callWebserviceASMX("Upload",params);
} catch (Exception e) {
e.printStackTrace();
}
}
/**调用webservice.asmx接口
*
* @param method 需要调用的方法
* @param json 传递的参数
*/
public static void callWebserviceASMX(String method,Object[] json){
//webservice接口地址
String url = "http://localhost/WebService.asmx";
//域名地址,server定义的
String soapaction = "http://tempuri.org/";
Service service = new Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
//设置要调用的方法
call.setOperationName(new QName(soapaction,method));
//设置要返回的数据类型
call.setReturnType(new QName(soapaction,method), String.class);
//设置参数值
call.addParameter(new QName(soapaction, "key"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "acc"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "pwd"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "xml"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setUseSOAPAction(true);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// (标准的类型)
call.setSOAPActionURI(soapaction+method);
//调用方法并传递参数
String result = (String) call.invoke(json);
System.out.println("result is:::"+result);
} catch (Exception e) {
e.printStackTrace();
}
}
}