ws发布后rpc的几种调用方式。
导入的几个jar:
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
1、指明返回值类型的调用
public static void testxml() throws Exception{ QName qname = new QName(" http://services.idcmanage.police.paibo.com/","addHostRoomFromIdc "); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<Body Description=\"IpConfig\" Code=\"0\">\n" +" <Item ID=\"32132_qq\" CARRIERS=\"3123123\" RECORD_NUM=\"123123\" NAME=\"11111\" ADDRESS=\"123123\" LINK_NAME=\"aaa\" LINK_TEL=\"123\" ID_CARD=\"123\" PERMIT_CODE=\"1323\" SERVER_NUM=\"0\" IDC_SERVER=\"9\" DOMAIN_NUM=\"100\" IP_RANGE=\"192.1.1.2\" SAFE_INTERNET_INSTITUTION=\"qwe\" SAFE_INTERNET_TECHNIQUE=\"qwe\" idc_code=\"310111111\"/> \n" +" <Item ID=\"32132_qq\" CARRIERS=\"3123123\" RECORD_NUM=\"123123\" NAME=\"11111\" ADDRESS=\"123123\" LINK_NAME=\"aaa\" LINK_TEL=\"123\" ID_CARD=\"123\" PERMIT_CODE=\"1323\" SERVER_NUM=\"0\" IDC_SERVER=\"9\" DOMAIN_NUM=\"100\" IP_RANGE=\"192.1.1.2\" SAFE_INTERNET_INSTITUTION=\"qwe\" SAFE_INTERNET_TECHNIQUE=\"qwe\" idc_code=\"310111111\"/> \n" + "</Body>"; Object[] param = new Object[] { xml }; String result=""; RPCServiceClient client =null; try { client = new RPCServiceClient(); Options options = new Options(); options.setAction("urn:addHostRoomFromIdc"); options.setTo(new EndpointReference(" http://192.168.1.151:9999/services/SendIdcManageService/?wsdl ")); client.setOptions(options); Class[] classtmp = new Class []{String.class};//输出返回值内容 result = (String) ((Object[])client.invokeBlocking(qname,param, classtmp))[0]; System.out.println(result); client.cleanupTransport(); } catch (AxisFault e) { e.printStackTrace(); logger.error("********sendSMSofSx:",e); }catch (Exception e) { e.printStackTrace(); logger.error("********sendSMSofSx:",e); }finally{ if(client!=null){ client.cleanupTransport(); } } }
2、无指定返回值类型的调用,此时发布的ws参数必须为默认的arg0
public static void testxml() throws Exception{ QName qname = new QName(" http://services.idcmanage.police.paibo.com/","addHostRoomFromIdc "); String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<Body Description=\"IpConfig\" Code=\"0\">\n" +" <Item ID=\"32132_qq\" CARRIERS=\"3123123\" RECORD_NUM=\"123123\" NAME=\"11111\" ADDRESS=\"123123\" LINK_NAME=\"aaa\" LINK_TEL=\"123\" ID_CARD=\"123\" PERMIT_CODE=\"1323\" SERVER_NUM=\"0\" IDC_SERVER=\"9\" DOMAIN_NUM=\"100\" IP_RANGE=\"192.1.1.2\" SAFE_INTERNET_INSTITUTION=\"qwe\" SAFE_INTERNET_TECHNIQUE=\"qwe\" idc_code=\"310111111\"/> \n" +" <Item ID=\"32132_qq\" CARRIERS=\"3123123\" RECORD_NUM=\"123123\" NAME=\"11111\" ADDRESS=\"123123\" LINK_NAME=\"aaa\" LINK_TEL=\"123\" ID_CARD=\"123\" PERMIT_CODE=\"1323\" SERVER_NUM=\"0\" IDC_SERVER=\"9\" DOMAIN_NUM=\"100\" IP_RANGE=\"192.1.1.2\" SAFE_INTERNET_INSTITUTION=\"qwe\" SAFE_INTERNET_TECHNIQUE=\"qwe\" idc_code=\"310111111\"/> \n" + "</Body>"; Object[] param = new Object[] { xml }; String result=""; RPCServiceClient client =null; try { client = new RPCServiceClient(); Options options = new Options(); options.setAction("urn:addHostRoomFromIdc"); options.setTo(new EndpointReference(" http://192.168.1.151:9999/services/SendIdcManageService/?wsdl ")); client.setOptions(options); OMElement element = client.invokeBlocking(qname, param); result = element.getFirstElement().getText(); System.out.println(result); client.cleanupTransport(); } catch (AxisFault e) { e.printStackTrace(); logger.error("********sendSMSofSx:",e); }catch (Exception e) { e.printStackTrace(); logger.error("********sendSMSofSx:",e); }finally{ if(client!=null){ client.cleanupTransport(); } } }
3、上两种方式都无法适用自定义的ws方式。特别是自定义的方法参数名称,即适用@WebParam(name="xml")
public static void testxml() throws Exception{ String path = "D:\\idcxml\\"+"addHostRoomFromIdc"+".xml"; File file = new File(path); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String xml = ""; String readline = ""; while((readline=br.readLine())!=null){ xml = readline; System.out.println(xml); } String result=""; RPCServiceClient client =null; try { client = new RPCServiceClient(); Options options = new Options(); options.setTo(new EndpointReference(" http://192.168.1.151:9999/services/SendIdcManageService/?wsdl ")); client.setOptions(options); // 创建一个OMFactory,下面的namespace、方法与参数均需由它创建 OMFactory factory = OMAbstractFactory.getOMFactory(); // 创建命名空间 OMNamespace namespace = factory.createOMNamespace(" http://services.idcmanage.police.paibo.com/ ", "urn"); // 参数对数 ******重要*******有些是指定方法名的。增加之后可以自定义参数名称 OMElement nameElement = factory.createOMElement("arg0", null); nameElement.addChild(factory.createOMText(nameElement, xml)); // 创建一个method对象 OMElement method = factory.createOMElement("addHostRoomFromIdc", namespace); method.addChild(nameElement); OMElement resultOM = client.sendReceive(method); result = resultOM.getFirstElement().getText(); System.out.println(result); client.cleanupTransport(); } catch (AxisFault e) { e.printStackTrace(); logger.error("********sendSMSofSx:",e); }catch (Exception e) { e.printStackTrace(); logger.error("********sendSMSofSx:",e); }finally{ if(client!=null){ client.cleanupTransport(); } } }
推荐适用第三种方式,ws的请求就可以轻松自定义(都是测试通过的,根据自己需要调整)
原文地址:http://xuanxy.blog.51cto.com/2357481/1592691