客戶端呼叫webservice的xfie和aixs2用法
阿新 • • 發佈:2018-12-20
第一種:
xfire:
public String test1(){ // SoapController(客戶端自己定義的介面) 為服務端暴露出來的方法,方法名和引數必須和服務端的匹配 Service srvcModel = new ObjectServiceFactory() .create(SoapController.class); XFireProxyFactory factory = new XFireProxyFactory(XFireFactory .newInstance().getXFire()); SoapController srvc = null; try { srvc = (SoapController) factory.create(srvcModel, "http://XXX?wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); } String hello = srvc.hello(); System.out.println(hello); }
第二種:
xfire:
public String test2(){ StringWriter sw = new StringWriter(); Object[] objects = null; // Client client = new Client(new URL(interfaceWsdl)); URL url = new URL("http://XXX?wsdl"); HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection(); httpConnection.setReadTimeout(120000);//設定http連線的讀超時,單位是毫秒,根據實際情況指定 httpConnection.connect(); Client client = new Client(httpConnection.getInputStream(), null); client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, String.valueOf( 120000));//設定傳送的超時限制,單位是毫秒,根據實際情況指定 client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true"); client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true"); Object[] opAddEntryArgs = new Object[] { "", "", "" };//引數列表 try { objects = client.invoke("方法名稱", opAddEntryArgs); //呼叫介面 } catch (Exception e) { e.printStackTrace(new PrintWriter(sw, true)); } return objects[0].toString(); }
第三種:
axis2:
public static String test3() throws Exception { // 使用RPC方式呼叫WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); options.setProperty(HTTPConstants.CHUNKED, "false");//設定不受限制. // 指定呼叫WebService的URL EndpointReference targetEPR = new EndpointReference("http://XXX");//wsdl的地址,不帶wsdl options.setTo(targetEPR); // 指定sayHelloToPerson方法的引數值 Object[] opAddEntryArgs = new Object[] { "", "",""};//引數列表 // 指定方法返回值的資料型別的Class物件 Class[] classes = new Class[] { String.class }; // 指定要呼叫的sayHelloToPerson方法及WSDL檔案的名稱空間 QName opAddEntry = new QName("http://XXX", "方法名");//xxx表示名稱空間 String str = (String) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]; return str; }