WebService学习2:服务端发布服务、客户端调用服务
1.基础知识
2. ?示例步骤
3. 客户端使用js+xml调用service
<html> <head> <title>通过ajax调用WebService服务</title> <script> var xhr = new ActiveXObject("Microsoft.XMLHTTP");//获得IE的xmlHttpRequest对象 function sendMsg(){ var name = document.getElementById('name').value; //服务的地址 var wsUrl = 'http://192.168.1.100:6789/hello'; //★请求体 var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' + ' <soapenv:Body> <q0:sayHello><arg0>'+name+'</arg0> </q0:sayHello> </soapenv:Body> </soapenv:Envelope>'; //打开连接 xhr.open('POST',wsUrl,true); //重新设置请求头 xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8"); //设置回调函数 xhr.onreadystatechange = _back; //发送请求 xhr.send(soap); } function _back(){ if(xhr.readyState == 4){ if(xhr.status == 200){ //alert('调用Webservice成功了'); var ret = xhr.responseXML; var msg = ret.getElementsByTagName('return')[0]; document.getElementById('showInfo').innerHTML = msg.text; //alert(msg.text); } } } </script> </head> <body> <input type="button" value="发送SOAP请求" onclick="sendMsg();"> <input type="text" id="name"> <div id="showInfo"> </div> </body> </html> 4. 通过URLConnection方式调用WebService//基本上和ajax差不多,只是表现方式不同 public class App { public static void main(String[] args) throws Exception { //服务的地址 URL wsURL = new URL("http://192.168.1.115:1120/hello"); HttpURLConnection conn = (HttpURLConnection) wsURL.openConnection(); conn.setDoInput(true);//有注入 conn.setDoOutput(true);//有输出 conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type","text/xml;charset=UTF-8"); OutputStream os = conn.getOutputStream();//得到输出流 //设置请求体 String soap = "";//略 os.write(soap.getBytes()); InputStream is = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; String s = ""; while((len = is.read())!=-1){ String ss = new String(b,len,"UTF-8"); s+=ss; } System.err.println(s); is.close(); os.close(); conn.disconnect(); } } 5. 解读wsdl文件
5. 附录:本示例服务的wsdl文件<?xml version="1.0" encoding="UTF-8" ?> <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.hw.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.hw.cn/" name="HelloServiceService"> <types> <xsd:schema> <xsd:import namespace="http://ws.hw.cn/" schemaLocation="http://192.168.1.115:1120/hello?xsd=1" /> </xsd:schema> </types> <message name="sayHello"> <part name="parameters" element="tns:sayHello" /> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse" /> </message> <portType name="HelloService"> <operation name="sayHello"> <input wsam:Action="http://ws.hw.cn/HelloService/sayHelloRequest" message="tns:sayHello" /> <output wsam:Action="http://ws.hw.cn/HelloService/sayHelloResponse" message="tns:sayHelloResponse" /> </operation> </portType> <binding name="HelloServicePortBinding" type="tns:HelloService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> <operation name="sayHello"> <soap:operation soapAction="" /> <input> <soap:body use="literal" /> </input> <output> <soap:body use="literal" /> </output> </operation> </binding> <service name="HelloServiceService"> <port name="HelloServicePort" binding="tns:HelloServicePortBinding"> <soap:address location="http://192.168.1.115:1120/hello" /> </port> </service> </definitions> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |