这篇文章用于建立对于SOAP的基本概念。
当明白了这些基本概念后,就很容易理解java中相应的类以及它们的用途了。
因此看完这篇文章后,再看下一篇博客:SOAP在java中的应用实例。
之后就可以开始写代码了。其实SOAP真的很简单。
如果对web services缺乏基本的概念,也可以参考另一篇博客:web services 简介。非常简单好理解。
下面的链接是w3c的SOAP标准的官方链接,其中的一些细节值得注意。
http://www.w3.org/TR/2000/NOTE-SOAP-20000508/
SOAP is a simple XML-based protocol to let applications exchange information over HTTP.
Or more simply: SOAP is a protocol for accessing a Web Service.
因此SOAP是基于HTTP的,原因很简单。HTTP是internet中使用最广泛的协议,使用HTTP协议,可以更方便的在internet中的不同部分交换数据。
我们在后面将看到,SOAP是如何使用HTTP协议的。
和上文说的一样,SOAP虽然是标准协议,但现在其实是专门用于web services通信的,并没有应用到其他的方面。
更简单一点说:SOAP是访问web services的标准协议。
什么是SOAP?
- SOAP stands for Simple Object Access Protocol
- SOAP is a communication protocol
- SOAP is for communication between applications
- SOAP is a format for sending messages
- SOAP communicates via Internet
- SOAP is platform independent
- SOAP is language independent
- SOAP is based on XML
- SOAP is simple and extensible
- SOAP allows you to get around firewalls
- SOAP is a W3C recommendation
A better way to communicate between applications is over HTTP,because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this.
SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.
SOAP的语法
下面的例子就是一个完整的SOAP message:
由Envelope,header, Body and fault四部分组成,绿色部分是固定不变的。
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header> //header是optional的
...
</soap:Header>
<soap:Body>
...
<soap:Fault> //fault也是可以没有的部分
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
可以将SOAP想象成一个信封(即Envelope),里面包含了header, body and fault。
- 1,SOAP Envelope element is the root element of a SOAP message. This element defines the XML document as a SOAP message.
- 2,The optional SOAP Header element contains application-specific information (like authentication, payment, etc) about the SOAP message.
The attributes defined in the SOAP Header defines how a recipient should process the SOAP message.
上句的意思是在header中定义的attribute,定义了SOAP message的接受者如何处理这个SOAP mesaage
下面是现实中header的一个例子:
< h:ApplicationCode xmlns:h="http://services/2010-03-01/" xmlns="http://services/2010-03-01/">TEST</h:ApplicationCode>
<h:ClientDetails xmlns:h="http://services/2010-03-01/" xmlns="http://services/2010-03-01/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MachineDetails xmlns="http://schemas">3M00753-BFEBFBFF0001067A</MachineDetails>
<ProductVersion xmlns="http://schemas">PROGRAM 3.1</ProductVersion>
</h:ClientDetails>
<h:RequestId xmlns:h="http://services/2010-03-01/" xmlns="http://services/2010-03-01/">94575ef8-6f4f-446e-b8e9-01abec964837</h:RequestId>
<userIdentity xmlns="http://schemas/2007/10/cp/user_identity">
<UUID>UserID1</UUID>
<SUBID/>
</userIdentity>
</s:Header>
可见header包含了要访问的web services所暴露的程序所在机器,程序名称为PROGRAM 3.1,以及用户验证信息。
留意到上面的s: 以及 h:和xmlns是和XML namespace相关的内容。
其中s:h:是prefix。 xmlns:h是h:对应的namespace,xmlns是默认的namespace。不清楚的同学自己去查资料。
SOAP defines three attributes in the default namespace ("http://www.w3.org/2001/12/soap-envelope"). These attributes are: mustUnderstand, actor, and encodingStyle.
但可以根据需要使用。
mustUnderstand attribute 表示header是否是接收者必须处理的信息。
encodingStyle attribute is used to define the data types used in the document:soap:encodingStyle="URI"
- 3,The SOAP Body element contains the actual SOAP message.可以理解为,具体向web services请求的服务是什么。
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LoginRequest xmlns="http://schemas/Messages/Base/2010-03-01/"/>
</s:Body>
上面为请求登陆PROGRAM 3.1系统。注意这里的LoginRequest是PROGRAM 3.1提供的功能,使用WSDL可以浏览要访问的web services暴露了PROGRAM 3.1的哪些功能。
- 4,SOAP fault部分通常在发送SOAP request后,web services的response里会出现。如果发送的SOAP request有问题,web services会返回一个SOAP Message,其中包含了错误信息。
SOAP HTTP Binding
前面提到了,SOAP message在外面被HTTP又包了一层,为了更方便的在internet上传播。SOAP HTTP Binding讲述的就是这个包装的过程。
关于HTTP request的知识可以了解:
A SOAP method is an HTTP request/response that complies with the SOAP encoding rules.
HTTP + SOAP message= 完整的SOAP request
了解HTTP的同学都知道,HTTP request由三部分组成: request line ( 其实就是这么一行POST /item HTTP/1.1 ,这里也可以是GET), header, body。
所以完整的SOAP request也必须包含这三部分。其中body就是SOAP message本身。
A SOAP request could be an HTTP POST or an HTTP GET request.
The HTTP POST request specifies at least two HTTP headers: Content-Type and Content-Length.
POST /item HTTP/1.1 这里也可以是get,这行就是http request line
Host: 189.123.345.239 从这行开始的3行是http header
Content-Type: text/plain
Content-Length: 200
The server then processes the request and sends an HTTP response back to the client.返回的response也符合标准的HTTP协议。
200 OK
Content-Type: text/plain
Content-Length: 200
200 OK是最常见的返回结果,意思是成功。
但是和标准的HTTP request不同的是, SOAP中的http头必须包含这样一行(在上面的w3c文档有相关描述):
SOAPAction: "http://ASAP.services.tfn.**.com/2010-03-01/Login"
The SOAPAction HTTP request header field can be used to indicate the intent of the SOAP HTTP request. The value is a URI identifying the intent.
An HTTP client MUST use this header field when issuing a SOAP HTTP Request.
最后我们来看一个完整的SOAP request
request:
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
SOAPAction: "http://ASAP.services.tfn.**.com/2010-03-01/Login"
Content-Length: nnn //下面一定会有一个空行将http header和http body (这里就是SOAP Message) 隔开。这是http协议的规定。
//但之后的SOAP message只要符合XML协议就好,其中是否有空行是无关紧要的。
<?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>
请求查询IBM的股票价格。
The SOAP response:
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>
在response中,我们发现,没有header element。 同时在SOAP body中看到从服务器查询出来的价格。