SOAP概述和结构

    技术2022-05-20  40

    在介绍WSDL时,指出web服务传送消息的协议是SOAP,那soap是什么呢?

    SOAP(Simple Object Access Protocol),简单对象访问协议,作用就是规定发送消息的格式和使用HTTP进行消息交换。因为它规定使用HTTP进行应用间的通信,而所有浏览器和服务器都支持HTTP,所以SOAP就成为了一种应用广泛的通信方法。

    SOAP消息格式:

    SOAP消息用xml编码、必须有的元素是SOAP envelope和encoding,结构如下

    <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Header> ... </soap:Header> <soap:Body> ... <soap:Fault> ... </soap:Fault> </soap:Body> </soap:Envelope> 

    补充:Envelope把此xml文档标识为一条soap消息,xmlns:soap命名空间始终为http://www.w3.org/2001/12/soap-envelope,soap:econdingStyle定义文档使用的数据类型,可出现在任何soap元素中,必有;Header包含头部信息,可选;Body为具体的消息内容,即最终端点需要的消息,必需;Fault用于指示错误消息。

    举例:

    <!--请求消息--> <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body> <!--m:getprice等是应用自己的专用元素,不属于soap标准--> <m:GetPrice xmlns:m="http://www.w3school.com.cn/prices"> <m:Item>Apples</m:Item> </m:GetPrice> </soap:Body> </soap:Envelope> <!--响应消息--> <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body> <m:GetPriceResponse xmlns:m="http://www.w3school.com.cn/prices"> <m:Price>1.90</m:Price> </m:GetPriceResponse> </soap:Body> </soap:Envelope> 

    HTTP通信:

    Http在TCP/IP上进行通信,格式为HTTP头、空行、body。头必须的有Content-Type和Content-Length,另外还有HTTP方法(get/post)和HTTP版本。SOAP消息就放在HTTP的body中。

    <!--soap请求--> POST /InStock HTTP/1.1 Host: www.example.org Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPrice> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope> <!--soap响应--> HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPriceResponse> <m:Price>34.5</m:Price> </m:GetStockPriceResponse> </soap:Body> </soap:Envelope>


    最新回复(0)