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()+" 文件下載完成");
}
}