webService 客户端调用 axis2
发布时间:2020-12-17 00:21:27 所属栏目:安全 来源:网络整理
导读:今天找出来给同事用,重新改了下 给同事.?? 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.
今天找出来给同事用,重新改了下 给同事.?? 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; ?? ?} }(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |