之前我们使用过HttpWatch获取的HTTP的调用过程,并获得了HTTP的请求头及其他请求的详细信息。 既然WebServie也是通过HTTP进行通信的,能不使用HTTPWatch来获取它的请求过程呢? 我们的代码不仅仅是向服务器发送的HTTP协议,更具体的说应该叫SOAP协议,它是WebService进行通信的基础。 为了获取SOAP数据发送和接收的格式。我们有必要使用一个工具来深入的了解WebService. 为了监控拦截请求头和响应头的具体数据,我们使用TCP/IP Monitor来拦截请求和响应的完整过程。
1:简单的说,SOAP就是在HTTP的基础上传输XML数据,以实现远程调用。 因为HTTP和XML格式的数据已经被广泛的应用。而SOAP又架构在这两种技术之上,所以WebService为什么会流行也就不难理解了。 2:老生常谈,无论你的服务端是什么语言书写的,只要接收SOAP协议的XML数据,并返回SOAP协议的XML数据,就可以被任何语言调用。 3、以下是通过纯ajax向服务器发送XML数据并解析的代码: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <p>通过Ajax向服务器发送XML数据</p> <button onclick="test1();">Ajax</button> </body> <script type="text/javascript"> var http; function test1(){ if(window.XMLHttpRequest){ http = new XMLHttpRequest(); }else{ http = new ActiveXObject("Microsoft.XMLHttp"); } var url = "One"; http.open("POST",url,true); http.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); http.onreadystatechange=function(){ if(http.readyState==4){ if(http.status==200){ //返回JSON var json = http.responseText; var person= eval("("+json+")"); alert("人员名单:"+person.name); } } }; http.send("<?xml version='1.0' encoding='utf-8'?>" + "<user><name>王健</name><name>张三</name></user>"); } </script> </html> --- 服务器使用jaxp进行解析 package cn.itcast; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * 接收XML数据使用jaxp进行解析 * @author 王健 */ public class One extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取输入流,如果在输出,请在组成String时使用UTF-8 InputStream in = request.getInputStream(); String names = ""; // 使用JAXP解析 try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = builder.parse(in); NodeList nl = dom.getElementsByTagName("name"); for(int i=0;i<nl.getLength();i++){ Element el = (Element)nl.item(i); String name = el.getTextContent(); System.err.println(">>:"+name); names +=name; } } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } response.setContentType("text/xml;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print("{"name":""+names+""}"); } } -----以下通过jQuery+Dom4j实现发XML数据-------- <script type="text/javascript" src="js/jquery-1.6.2.js"></script> <script type="text/javascript"> $(function(){ $("#jq").click(function(){ var xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<user><name>王健A</name><name>张三</name></user>"; alert(xml); $.ajax({ url:'Two', type:'post', dataType:'json',//设置返回的数据类型 data:xml,//直接发xml数据 contentType:'application/x-www-form-urlencoded', success:function(data){ alert("返回的信息是:"+data.name); }, complete:function(http,textStatus){ alert("over..."+textStatus); } }); }); }); </script> ---------------------------- package cn.itcast; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * JQuery+Dom4j * @author 王健 */ public class Two extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String result = ""; //中文new String(b,0,len,"UTF-8") InputStream in = request.getInputStream(); //以下用dom4j解析 SAXReader sax = new SAXReader(); try { Document dom = sax.read(in); Element root = dom.getRootElement(); Iterator<Element> it = root.elementIterator(); while(it.hasNext()){ String nm = it.next().getStringValue(); System.err.println(nm); result+=nm; } } catch (Exception e) { throw new RuntimeException(e.getMessage(),e); } response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //返回json数据 out.print("{"name":""+result+""}"); } }