標籤:webservice xfire javascript web服務
一 、通過Web服務端提供的介面來建立用戶端
/** * 通過Web服務端提供的介面來建立用戶端 * @see 用戶端必須提供一個與服務端完全一致的介面,包名也要一致 * @see 並且此時需要在項目中引入XFire 1.2 Core Libraries和XFire 1.2 HTTP Client Libraries */ public class PortClient {public static void main(String args []){//首先使用XFire的ObjectServiceFactory從HelloService介面建立一個服務模型serviceModel //serviceModel包含服務的說明,換句話說,就是服務的中繼資料 Service serviceModel = new ObjectServiceFactory().create(KingTeamService.class); //訪問的地址 String serviceURL = "http://localhost:80/xfireD/services/KingTeamService"; //為XFire獲得一個代理工廠對象 XFireProxyFactory factory = new XFireProxyFactory(); try { //通過proxyFactory,使用服務模型serviceModel和服務端點URL(用來獲得WSDL) //得到一個服務的本地代理,這個代理就是實際的用戶端 KingTeamService client = (KingTeamService)factory.create(serviceModel, serviceURL);/** * Invoke the service * @see 調用服務的本地代理(即實際的用戶端)中的方法,便得到我們需要的WebServcie */ int serviceResponse = client.jian(12,3); System.out.println(serviceResponse);} catch (MalformedURLException e) {e.printStackTrace();} }}
二、通過WSDL地址來建立動態用戶端
public class KingTeamClient {public static void main(String args []){try {Client client = new Client(new URL("http://localhost:80/xfireD/services/KingTeamService?wsdl"));Object[] k = client.invoke("jia", new Object[]{12,12});System.out.println(k[0]);} catch (MalformedURLException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}
三、web頁面JavaScript
JavaScirpt 代碼:
<script type="text/javascript">function invokeServerFunction(){ var xmlHttp; try{ xmlHttp = new XMLHttpRequest(); }catch(e){ try{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("很遺憾, 您的瀏覽器不支援AJAX, 請使用其它瀏覽器, 如Firefox、Safari、Opera8.0+"); return false; } } } xmlHttp.onreadystatechange = function(){ if(4 == xmlHttp.readyState){ alert(xmlHttp.status); if(200 == xmlHttp.status){ //document.writeln("Web服務所返回的結果為:" + xmlHttp.responseText); document.getElementById("result").innerHTML = xmlHttp.responseText; } } }; var data; data = '<?xml version="1.0" encoding="UTF-8"?>'; //這裡要注意,不建議寫成下面這個樣子,否則會報錯 //data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" // xmlns:xsd="http://www.w3.org/2001/XMLSchema" // xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'; //最好把這三個東西,寫成一行 data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'; data = data + '<soap:Body>'; //這裡的<sayHello>標籤,對應的是用戶端所要調用的服務端的方法 data = data + '<hello xmlns="com.yao.xfire.service/KingTeamServer">'; //這裡的<str>標籤,對應的是傳遞給sayHello()方法的參數 data = data + '<str>12,3</str>'; data = data + '</hello>'; data = data + '</soap:Body>'; data = data + '</soap:Envelope>'; //這是URL,即訪問的地址,其格式為http://IP:連接埠/服務端項目/services/服務端提供的服務對外開放的名字 var url = "http://127.0.0.1/xfireD/services/KingTeamService"; xmlHttp.open("POST", url, true); xmlHttp.setRequestHeader ("Content-Type","text/xml; charset=UTF-8"); //這裡的第二個參數,是服務端在services.xml中指定的<namespace>加上所要調用的sayHello()方法名 xmlHttp.setRequestHeader ("SOAPAction","http://com.yao.xfire.service/KingTeamService/hello"); xmlHttp.send(data); } </script>
頁面HTML代碼:
<body> <input type="button" value="viewResult" onclick="invokeServerFunction()" /><br/> <br/>Web服務所返回的結果為:<span id="result"></span> </body>
用戶端調用Xfire WebService(含JavaScript)