rpc调用webservice

ws发布后rpc的几种调用方式。java

 导入的几个jar:apache

import javax.xml.namespace.QName;ide

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;测试

一、指明返回值类型的调用spa

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();

   }

  }

 }

 

二、无指定返回值类型的调用,此时发布的ws参数必须为默认的arg0.net

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();

   }

  }

 }

 

 

 

三、上两种方式都没法适用自定义的ws方式。特别是自定义的方法参数名称,即适用@WebParam(name="xml")code

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 {    
//System.setProperty("javax.net.ssl.trustStore", "d:\\idcxml\\idcstore.keystore"); 
//若是是https类型的ws,此句话解开便可访问(d:\\idcxml\\idcstore.keystore是你发布的路径)
   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的请求就能够轻松自定义(都是测试经过的,根据本身须要调整)xml

 

 

 四、另一种调用方式对象

package client;ip


import java.io.FileOutputStream;

import java.util.Date;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;


/*

 * 仅适用于小附件上传、下载,10M如下。

 */

public class BlobRPCClient2

{

    public static void main(String[] args) throws Exception

    {

        RPCServiceClient serviceClient = new RPCServiceClient();

        Options options = serviceClient.getOptions();

        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/BlobService");

        options.setTo(targetEPR);

       

        //=================测试文件上传==================================

        

        String filePath = "f:\\head.jpg";

        DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));

      

        //设置入参(一、文件名,二、DataHandler)

        Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", dataHandler};

        

        //返回值类型

        Class<?>[] classes = new Class<?>[]{ Boolean.class };

        

        //指定要调用的方法名及WSDL文件的命名空间

        QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadFile");

     

        //执行文件上传

        System.out.println(new Date()+" 文件上传开始");

        Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];

        System.out.println(new Date()+" 文件上传结束,返回值="+returnValue);

      

        //=================测试文件下载==================================


        opAddEntry = new QName("http://ws.apache.org/axis2", "downloadFile");

        opAddEntryArgs = new Object[]{};

        classes =  new Class<?>[]{ DataHandler.class };

        

        System.out.println(new Date()+" 文件下载开始");

        DataHandler returnHandler = (DataHandler) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];

        FileOutputStream fileOutPutStream = new FileOutputStream("F:\\我是下载的文件.jpg");

        returnHandler.writeTo(fileOutPutStream);

        fileOutPutStream.flush();

        fileOutPutStream.close();

        System.out.println(new Date()+" 文件下载完成");

    }

}

相关文章
相关标签/搜索