今天找出来给同事用,重新改了下 给同事.
package com.jielan.axis2; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.transport.http.HTTPConstants; public class Client { //private static String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"; // targetEPR指定打包的Service(.aar文件)在容器中的物理位置。 //EndpointReference端点引用 private static EndpointReference targetEpr; //创建request SOAP包工厂 fac。 private static OMFactory fac = OMAbstractFactory.getoMFactory(); public static OMElement getmethodOMElement(String nameSpace,String tns,String methodName,String[] args,String[] vals){ // OMNamespace指定此fac SOAP文档名称空间。 //有一个tns的namespace,这个namespace和目标的接口有关,一定要和目标 wsdl的message传输接口一致 是tns OMNamespace omNs = fac.createOMNamespace(nameSpace,tns); //创建元素getWeatherbyCityName oap,并指定其在omNs指代的名称空间中。 方法函数 OMElement method = fac.createOMElement(methodName,omNs); //给method添加 这个方法的参数名,和参数值 for(int i =0;i<args.length;i++){ //新建参数OMElement对象实例,设置参数名 OMElement params = fac.createOMElement(args[i],omNs); //给参数名对象设置参数值 params.setText(vals[i]); //将参数OMElement对象添加到Method对象中 method.addChild(params); } //返回request的方法 SOAP包 return method; } public static void main(String[] args) throws AxisFault { OMElement result = new Client().getResult("http://**********/EnterpriseService.asmx?wsdl", "http://tempuri.org/method","http://tempuri.org/","tns","method", new String[]{"paramName"},new String[]{"value"},60000); System.out.println(result.toString()); } /**** * * @param url webService wsdl地址 * @param SOAPAction SOAPAction的地址在方法中 请求头里面有SOAPAction: "http://tempuri.org/*******" * @param nameSpace 命名空间 xmlns="http://tempuri.org/" * @param tns 为空好像也可以 我是用 "tns" * @param method 方法名 * @param params 方法传入参数名称 * @param values 方法传入参数值 * @return 返回结果 OMElement 类型 * @throws AxisFault */ public OMElement getResult(String url,String SOAPAction,String nameSpace,String method,String[] params,String[] values,int waitTime) throws AxisFault{ targetEpr = new EndpointReference(url); ServiceClient client = new ServiceClient(); Options options = new Options(); options.setTimeOutInMilliSeconds(waitTime); options.setAction(SOAPAction); options.setTo(targetEpr); options.setTransportInProtocol(Constants.TRANSPORT_HTTP);//设定传输协议 options.setProperty(HTTPConstants.CHUNKED,"false"); client.setoptions(options); OMElement method1= getmethodOMElement(nameSpace,tns,method,params,values); OMElement result = client.sendReceive(method1); return result; } }版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。