我们上次使用客户端向服务器端发送了两次WebService请求,分别是



(1)sayHi请求


向服务端发送一个name,然后服务端回复“你好”和对应的name以及现在的时间。



(2)getAllCats请求


获取所有的宠物猫的数据。



报文大致截图:


【WebService】8.SOAP协议深入详解_xml



我们使用拦截器拦截了以上两个请求的请求报文和回复报文,我们接下来通过一一剖析来深入理解SOAP。



A.对于sayHi操作:


传入消息

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope">
<S:Body>
<ns2:sayHi xmlns:ns2="http://ws.cxf.java.org">
<arg0>孙悟空</arg0>
</ns2:sayHi>
</S:Body>
</S:Envelope>


传出消息


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:sayHiResponse xmlns:ns2="http://ws.cxf.java.org">
<return>孙悟空,您好现在的时间是:Mon Aug 01 18:33:53 CST 2016</return>
</ns2:sayHiResponse>
</soap:Body>
</soap:Envelope>


B.对于getAllCats操作


传入消息


<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope">
<S:Body>
<ns2:getAllCats xmlns:ns2="http://ws.cxf.java.org"/>
</S:Body>
</S:Envelope>


传出消息


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getAllCatsResponse xmlns:ns2="http://ws.cxf.java.org">
<return>
<entries>
<key>第1个</key>
<value>
<color>黑色</color>
<id>1</id>
<name>花花</name>
</value>
</entries>
<entries>
<key>第2个</key>
<value>
<color>白色</color>
<id>2</id>
<name>毛球</name>
</value>
</entries>
<entries>
<key>第3个</key>
<value>
<color>黄色</color>
<id>3</id>
<name>丁丁</name>
</value>
</entries>
<entries>
<key>第4个</key>
<value>
<color>灰色</color>
<id>4</id>
<name>咪咪</name>
</value>
</entries>
</return>
</ns2:getAllCatsResponse>
</soap:Body>
</soap:Envelope>


以上是请求成功的返回信息,当我们启动服务端的时候,在浏览器输入提供服务的路径,而不输入服务名称,这个时候浏览器就会显示提示错误的soap信息:


【WebService】8.SOAP协议深入详解_soap_02



其实还有一个header元素,这个是自定义的,用来携带其他信息(如用户名密码)。



我们就可以分析出来SOAP信息的大致格式:


【WebService】8.SOAP协议深入详解_soap_03



我们其实可以看出来,body标签对的内容其实是和WSDL格式是对应的。



所以我们总结一下SOAP:


Header


    Header是可选的,由开发人员控制添加。


Body


    Body元素总是默认的,Body元素里可能有两种情况:


    --当WebService交互正确时,Body元素里的内容由WSDL控制。


    --当WebService交互出错时,Body元素里的内容为Fault子元素。