因工作需要,需要调用合作公司的一些接口方法。调了一下午才调通,记下来,以作备忘,不废话,直接代码。
import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class OrderProcessClient { public static int getRes(String user,String pwd,String orderNo,String xmlStr){ try { //xml地址(wsdl结尾) String endpoint = "http://wcf.v-grow.com/eTrade/OrderProcess.svc?wsdl"; Service service = new Service(); Call call = (Call) service.createCall(); //用户名密码验证 call.setUsername(user); call.setPassword(pwd); //call.getMessageContext().setUsername(user); //call.getMessageContext().setPassword(pwd); call.setTargetEndpointAddress(endpoint); //a:首行targetNamespace值 b:调用方法名 call.setOperationName(new QName("http://tempuri.org/","Accept")); call.setUseSOAPAction(true); //所调用方法里面的wsdl:input wsaw:Action值 call.setSOAPActionURI("http://tempuri.org/IOrderProcess/Accept"); //a:首行targetNamespace值 b:参数名,必须与.net设置的参数名相同 call.addParameter(new QName("http://tempuri.org/", "msgId"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.OUT);//接口的参数 call.addParameter(new QName("http://tempuri.org/", "orderNo"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);//接口的参数 call.addParameter(new QName("http://tempuri.org/", "xmlStr"), org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);//接口的参数 //设置返回类型 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT); //给方法传递参数,并且调用方法 int i = (Integer)call.invoke(new Object[]{orderNo,xmlStr}); return i; } catch (Exception e) { e.printStackTrace(); } return 2; } public static void main(String[] args) { String user = "username"; String pwd = "password"; String orderNo = "123"; StringBuilder xmlString = new StringBuilder(); xmlString.append("<Order orderid='123456'/>"); System.out.println(getRes(user,pwd,orderNo,xmlString.toString())); } }
jar包下载地址:http://download.csdn.net/detail/xlb744868186/8410873
时间: 2024-10-10 17:08:24