您的位置:首页 > Web前端 > JavaScript

客户端调用Xfire WebService(含JavaScript)

2015-03-09 15:45 232 查看
一 、通过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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: