客户端调用Xfire WebService(含JavaScript)
2015-03-09 15:45
232 查看
一 、通过Web服务端提供的接口来创建客户端
二、通过WSDL地址来创建动态客户端
三、web页面JavaScript
JavaScirpt 代码:
页面HTML代码:
/** * 通过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>
相关文章推荐
- WEBSERVICE XFIRE 服务器端的创建和客户端调用
- axis,jax-ws,xfire客户端调用分析,以及webservice万能客户端
- axis,jax-ws,xfire客户端调用分析,以及webservice万能客户端
- xfire webService发布以及客户端调用(带验证头)
- javascript,jquery调用xfire驱动的webservice
- Webservice使用xfire发布及客户端调用
- axis,jax-ws,xfire客户端调用分析,以及webservice万能客户端2562
- xfire 客户端调用webservice注意要点
- javascript,jquery调用xfire驱动的webservice
- webservice的axis2客户端调用方式和xfire调用方式
- xfire客户端调用webservice测试
- XFire+spring 发布webservice 及其客户端调用程序
- java调用xfire webService服务客户端代码
- Android 客户端调用基于XFire的webService方法传递自定义对象
- xfire 客户端调用webservice注意要点
- 在与sap系统集成时遇到的问题.sap系统发布一webservice,java(xfire)作为客户端调用,调用时抛出如下异常: Wrong Content-Type and empty HTTP-Body received: ("HTTP Code 20
- 采用spring,maven,xfire/cxf 实现SOAP的WebService,并用C#作为客户端调用
- 在JavaScript中调用WebService
- javascript 调用 WebService 的方法
- Javascript调用Webservice的汇集