SOAP协议
SOAP 基于XML 和 HTTP ,其通过XML 来实现消息描述,然后再通过 HTTP 实现消息传输(需要通过接口文档来了解提供者或消费者接口)。
WSDL
WSDL 基于 XML的用于描述 Web 服务以及如何访问 Web 服务的语言(统一接口,不管是请求者还是服务提供者,通过服务描述便可以不必了解对方的底层平台,编程语言等,服务描述与底层的 SOAP 基础结构相结合,足以封装服务请求者的应用程序和服务提供者的 Web服务之间的这个细节)。
WSDL提供“能办的事的文档说明”:对要提供的服务的一种描述格式。我想帮你的忙,但是我要告诉你我都能干什么,以及干这些事情需要的参数类型。
wsdl和soap虽然是web service的两大标准,但是两者并没有必然的联系,都可以独立使用。
wsdl提供了一个统一的接口,目前已经成为一个国际上公认的标准,通过wsdl提供的接口可以访问不同类型的资源(如java、c#、C、C、C++等),因为wsdl是基于xml,与语言平台无关的。另外wsdl提供了binding和service元素,用以绑定接口到具体的服务,实现了接口与实现的分离。
soap(简单对象访问协议)是一种基于http的传输协议,用来访问远程服务
wsdl与soap的关系在于:wsdl绑定服务的时候可以设定使用的协议,协议可以是soap、http、smtp、ftp等任何一种传输协议,除此以外wsdl还可以绑定jms、ejb及local java等等,不过都是需要对binding和service元素做扩展的,而且需要扩展服务器的功能以支持这种扩展
soap协议是一种请求和应答协议规范,而http是web传输协议,soap的传输是可以基于http的,但也可以基于其他的传输协议,如ftp、smtp等。
SOAP的数据格式
现在知道了SOAP是通过HTTP协议的POST方法来传输数据的,只不过是请求的Header中加了一些标志来说明自己是一个SOAP请求。
那么数据的具体格式是怎么规定的呢,我们把上面请求的XML数据展开看一下:
<? xml version="1.0" encoding="utf-8" ?>
< soap:Envelope xmlns:soap ="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd
="http://www.w3.org/2001/XMLSchema" >
< soap:Body >
< getSupportCity xmlns ="http://WebXml.com.cn/" >
< byProvinceName > 广东 </ byProvinceName >
</ getSupportCity >
</ soap:Body >
</ soap:Envelope >
其中的<soap:Body>里面的内容就是请求的内容,请求的方法为getSupportCity,该方法有一个名为
byProvinceName的参数,参数的值为“广东”这个字符串。再看一下返回的内容:
HTTP/1.1 200 OK
Date: Mon, 14 Dec 2009 05:55:39 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 1052
<? xml version="1.0" encoding="utf-8" ?>
< soap:Envelope xmlns:soap ="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
< soap:Body >< getSupportCityResponse xmlns ="http://WebXml.com.cn/" >
< getSupportCityResult >
< string > 广州 (59287) </ string >< string > 深圳 (59493) </ string >< string > 潮州 (59312) </ string >< string > 韶关 (59082) </ string >< string > 湛江 (59658) </ string >< string > 惠州 (59298) </ string >< string > 清远 (59280) </ string >< string > 东莞 (59289) </ string >< string > 江门 (59473) </ string >< string > 茂名 (59659) </ string >< string > 肇庆 (59278) </ string >< string > 汕尾 (59501) </ string >< string > 河源 (59293) </ string >< string > 揭阳 (59315) </ string >< string > 梅州 (59117) </ string >< string > 中山 (59485) </ string >< string > 德庆 (59269) </ string >< string > 阳江 (59663) </ string >< string > 云浮 (59471) </ string >< string > 珠海 (59488) </ string >< string > 汕头 (59316) </ string >< string > 佛山 (59279) </ string >
</ getSupportCityResult >
</ getSupportCityResponse >
</ soap:Body >
</ soap:Envelope >
返回的HTTP头中并没有标志来表明是一个SOAP的响应,因为的确没有必要,请求方发送出的SOAP请求,返回的肯定是SOAP的响应。
一个典型的SOAP请求格式的结构如下:
<? 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 >
< m:Trans xmlns:m ="http://www.w3schools.com/transaction/"
soap:mustUnderstand ="1" > 234
</ m:Trans >
</ soap:Header >
< soap:Body >
< m:GetPrice xmlns:m ="http://www.w3schools.com/prices" >
< m:Item > Apples </ m:Item >
</ m:GetPrice >
</ soap:Body >
</ soap:Envelope >
下面逐个解释里面的元素:
a) Envelope
SOAP的请求内容必须以Envelope做为根节点。
xmlns:soap="http://www.w3.org/2001/12/soap-envelope",不能修改,否则会出错。http://www.w3.org/2001/12/soap-envelope里面有Envelope的schema的相关定义。有兴趣的可以去这个链接的内容。
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding",这个指定了数据元素的类型。
b) Header
这个是可选的,如果需要添加Header元素,那么它必须是Envelope的第一个元素。
Header的内容并没有严格的限制,我们可以自己添加一些和应用程序相关的内容,但是客户端一定要记得处理这些Header元素,可以加上mustUnderstand强制进行处理。
c) Body
这个就是请求的主题内容了,请求什么函数,参数是什么类型等等都在这里面指定。
用标签表示一个函数,然后用子元素表示它的参数。
在调用中没有指定参数和返回类型,这里不需要指定,因为提供服务的一方自己已经规定好了数据类型,在调用时指定数据类型没有任何意义。
二、WSDL(Web Services Description Language)
WSDL是用来描述WebService的,它用XML的格式描述了WebService有哪些方法、参数类型、访问路径等等。我们要使用一个WebService肯定首先要获取它的WSDL,在VS中添加一个Web 引用时,这些工作由开发环境帮我们做了,开发环境根据WSDL文档给Web Service生成了相应的代理类供我们使用。
下面是一个HelloWorld的WebService的服务端代码:
public class Service : System.Web.Services.WebService
{
public Service () {
// Uncomment the following line if using designed components
// InitializeComponent();
}
[WebMethod]
public DateTime HelloWorld( int i)
{
return DateTime.Now;
}
}
其对应的WebService的WSDL文档如下:
<? xml version="1.0" encoding="utf-8" ?>
< wsdl:definitions xmlns:soap ="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm ="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc ="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime ="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns ="http://tempuri.org/" xmlns:s ="http://www.w3.org/2001/XMLSchema" xmlns:soap12 ="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http ="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace ="http://tempuri.org/" xmlns:wsdl ="http://schemas.xmlsoap.org/wsdl/" >
< wsdl:types >
< s:schema elementFormDefault ="qualified" targetNamespace ="http://tempuri.org/" >
< s:element name ="HelloWorld" >
< s:complexType >
< s:sequence >
< s:element minOccurs ="1" maxOccurs ="1" name ="i" type ="s:int" />
</ s:sequence >
</ s:complexType >
</ s:element >
< s:element name ="HelloWorldResponse" >
< s:complexType >
< s:sequence >
< s:element minOccurs ="1" maxOccurs ="1" name ="HelloWorldResult" type ="s:dateTime" />
</ s:sequence >
</ s:complexType >
</ s:element >
</ s:schema >
</ wsdl:types >
< wsdl:message name ="HelloWorldSoapIn" >
< wsdl:part name ="parameters" element ="tns:HelloWorld" />
</ wsdl:message >
< wsdl:message name ="HelloWorldSoapOut" >
< wsdl:part name ="parameters" element ="tns:HelloWorldResponse" />
</ wsdl:message >
< wsdl:portType name ="ServiceSoap" >
< wsdl:operation name ="HelloWorld" >
< wsdl:input message ="tns:HelloWorldSoapIn" />
< wsdl:output message ="tns:HelloWorldSoapOut" />
</ wsdl:operation >
</ wsdl:portType >
< wsdl:binding name ="ServiceSoap" type ="tns:ServiceSoap" >
< soap:binding transport ="http://schemas.xmlsoap.org/soap/http" />
< wsdl:operation name ="HelloWorld" >
< soap:operation soapAction ="http://tempuri.org/HelloWorld" style ="document" />
< wsdl:input >
< soap:body use ="literal" />
</ wsdl:input >
< wsdl:output >
< soap:body use ="literal" />
</ wsdl:output >
</ wsdl:operation >
</ wsdl:binding >
< wsdl:binding name ="ServiceSoap12" type ="tns:ServiceSoap" >
< soap12:binding transport ="http://schemas.xmlsoap.org/soap/http" />
< wsdl:operation name ="HelloWorld" >
< soap12:operation soapAction ="http://tempuri.org/HelloWorld" style ="document" />
< wsdl:input >
< soap12:body use ="literal" />
</ wsdl:input >
< wsdl:output >
< soap12:body use ="literal" />
</ wsdl:output >
</ wsdl:operation >
</ wsdl:binding >
< wsdl:service name ="Service" >
< wsdl:port name ="ServiceSoap" binding ="tns:ServiceSoap" >
< soap:address location ="http://localhost:2206/WebSite1/Service.asmx" />
</ wsdl:port >
< wsdl:port name ="ServiceSoap12" binding ="tns:ServiceSoap12" >
< soap12:address location ="http://localhost:2206/WebSite1/Service.asmx" />
</ wsdl:port >
</ wsdl:service >
</ wsdl:definitions >
一个WSDL文档由四部分组成:
1、types
指定了WebService用到的所有数据类型,上面用到了两种数据类型,int和datetime
2、message
指明一个操作所用到的数据类型。
HelloWorldSoapIn是指HelloWorld的输入操作用到的数据类型,HelloWorldSoapOut是指HelloWorld的输出操作用到的数据类型。二者的element元素指出了与types中对应到的具体类型。
3、portType
指出了这个WebService所有支持的操作,就是说有哪些方法可供调用。
这里支持一个HelloWorld调用,它的输入和输出对应到HelloWorldSoapIn和HelloWorldSoapOut这个两个数据类型。
4、binding
soap12:binding元素的transport指明传输协议,这里是http协议。
operation 指明要暴露给外界调用的操作。
use属性指定输入输出的编码方式,这里没有指定编码。
5、services
指定服务的一些信息,主要是指定服务的访问路径。
详细讲解