import javax.xml.namespace.QName; 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.rpc.client.RPCServiceClient; /** * axis2提供rpc和document两种style的实现。 * 在这里我们别对其验证。关于说明,请参看代码中的注释 * @author Administrator * */ public class CopyOfClient { public static void main(String[] args) { CopyOfClient client = new CopyOfClient(); //测试rpc方式 //client.testRPC(); //测试document方式 //client.testDocument(); client.testDocument1(); // client.testSubString(); } /** * 应用rpc的方式调用 * 这种方式就等于远程调用,即通过url定位告诉远程服务器,告知方法名称,参数等, * 调用远程服务,得到结果。 */ //下面这个annotaion是为了不让代码出现关于没有应用泛型的警告信息 //用elipse做编辑器的很容易理解。 @SuppressWarnings("unchecked") public void testRPC() { try { RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); //指定访问的web service地址 EndpointReference targetEPR = new EndpointReference( "http://localhost:8080/websServiceTest/services/HelloServiceNew"); options.setTo(targetEPR); //指定方法,注意指定命名空间 QName opPrint = new QName("http://webservice.sinosoft.com","add"); //确定参数类型和参数值 Class[] returnTypes = new Class[] { int.class }; Object obj[] = new Object[] { 1,2 }; //得到返回结果,是一个数组 Object[] order = serviceClient.invokeBlocking(opPrint,obj,returnTypes); System.out.println(order[0]); //下面是测试each方法的。和上面类似 opPrint = new QName("http://webservice.sinosoft.com","each"); returnTypes = new Class[] { String.class }; obj = new Object[] { "zhangyt" }; order = serviceClient.invokeBlocking(opPrint,returnTypes); System.out.println(order[0]); } catch (AxisFault e) { e.printStackTrace(); } } /** * 应用document方式调用 * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合 */ public void testDocument() { try { ServiceClient sc = new ServiceClient(); Options opts = new Options(); String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx"; //确定目标服务地址 opts.setTo(new EndpointReference(url)); //确定调用方法 opts.setAction("http://WebXml.com.cn/getWeatherbyCityName"); OMFactory fac = OMAbstractFactory.getOMFactory(); //指定命名空间 OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/",""); //指定方法 OMElement method = fac.createOMElement("getWeatherbyCityName",omNs); //指定方法的参数 OMElement value = fac.createOMElement("theCityName",omNs); value.setText("天津"); method.addChild(value); //OMElement value1 = fac.createOMElement("value",omNs); //value1.setText("2"); //method.addChild(value1); sc.setOptions(opts); //发送请求并并得到返回结果,注意参数生成方法的分析 OMElement res = sc.sendReceive(method); System.out.println(res); //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。 //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果 // res.getFirstElement().getText(); // System.out.println(res.getFirstElement().getText()); } catch (AxisFault e) { e.printStackTrace(); } } /** * 方法二: 应用document方式调用 * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合 */ public static void testDocument1() { try { // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService"; String url = "http://172.24.254.192/InspectionUp/services/ServiceServerImpl?wsdl"; Options options = new Options(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference(url); options.setTo(targetEPR); // options.setAction("urn:getPrice"); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); String tns = "http://webservice.bjits.org"; // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的 OMNamespace omNs = fac.createOMNamespace(tns,""); OMElement method = fac.createOMElement("getResourcesStatus",omNs); OMElement symbol = fac.createOMElement("type",omNs); OMElement symbols = fac.createOMElement("name",omNs); // 方法一 symbol.addChild(fac.createOMText(symbol,"http")); symbols.addChild(fac.createOMText(symbols,"webTest")); // 方法二 // method.addChild(symbol); // symbol.setText("1"); method.addChild(symbol); // // method.addChild(symbols); // symbols.setText("2"); method.addChild(symbols); method.build(); OMElement result = sender.sendReceive(method); System.out.println(result); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } } /** * 方法二: 应用document方式调用 * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合 */ public static void testSubString() { try { String url = "http://localhost:8080/websServiceTest/services/HelloServiceNew?wsdl"; Options options = new Options(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference(url); options.setTo(targetEPR); // options.setAction("urn:getPrice"); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); String tns = "http://webservice.sinosoft.com"; // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的 OMNamespace omNs = fac.createOMNamespace(tns,""); OMElement method = fac.createOMElement("add",omNs); //指定方法名 String all = "a=2&b=3";//模拟页面的 String[] split = all.split("&");//subString根据“&”截取字符窜 for(int i=0;i<split.length; i++) { // System.out.println(split[i]+"—"); //显示截取后的内容 String[] splits = split[i].split("=");// 使用=分割字符串 for(int a=0;a<splits.length -1; a++) { System.out.println(splits[0]+"="+splits[1]); //显示截取后的内容 OMElement symbol = fac.createOMElement(splits[0],omNs);//参数名称 symbol.addChild(fac.createOMText(symbol,splits[1]));//参数的值 method.addChild(symbol); } } // OMElement symbol = fac.createOMElement("a",omNs); // OMElement symbols = fac.createOMElement("b",omNs); //// 方法一 // symbol.addChild(fac.createOMText(symbol,"1")); // symbols.addChild(fac.createOMText(symbols,"2")); //// 方法二 //// method.addChild(symbol); //// symbol.setText("1"); // method.addChild(symbol); //// //// method.addChild(symbols); //// symbols.setText("2"); // method.addChild(symbols); method.build(); OMElement result = sender.sendReceive(method); System.out.println(result); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } } private static OMElement OMElement(Object setText) { // TODO Auto-generated method stub return null; } }
webservice-document 调用方法
发布时间:2020-12-16 23:48:59 所属栏目:安全 来源:网络整理
导读:import javax.xml.namespace.QName;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.apa
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |