我是用Axis做的调用、
1所需jar包
xmlsec-1.3.0.jar
axis.jar
axis-ant.jar
axis-schema-1.3.jar
axis-wsdl4j-1.5.1.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
mailapi-1.3.jar
saaj.jar
wsdl4j-1.5.1.jar
这些jar包可能建项目的时候会有,自己查看一下没有的就去下载。
2建一个类、如下是java代码。
import java.io.IOException;
import java.net.MalformedURLException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*@authorxiaoqiang
*
*测试调用WebService
*/
publicclass CaService {
privatestaticfinal Log log = LogFactory.getLog(CaService.class);
//这个是webService服务的地址
privatestaticfinal String ENDPOINT = "http://localhost/as/Service.asmx";
publicstaticvoid main(String[] args) {
CaService tester = new CaService();
tester.getServiceReturn();
}
publicvoid getServiceReturn() {
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(ENDPOINT));
//此处为webService的方法
call.setOperationName(new QName("http://tempuri.org/","GetVehicle"));
//此处为方法的输入变量
call.addParameter(new QName("http://tempuri.org/","VehicleNo"), org.apache.axis.Constants.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
call.setUseSOAPAction(true);
//soapAction地址
call.setSOAPActionURI("http://tempuri.org/GetVehicle");
//传入weService值并返回的值为ret
String ret = (String)call.invoke(new Object[] {"ggggg"});
System.out.println("The return value is:" + ret);
return;
} catch (Exception e) {
e.printStackTrace();
}
log.error("call service error!");
}
//java调用webService
publicvoid getReturn3(){
try {
String endpoint = "http://localhost:8888/WS/services/TransService";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("getComp");//WSDL里面描述的接口名称
call.addParameter("in0", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}