soap的理解
概念
soap就是简单对象访问协议,是基于 XML 的用于访问网络服务的协议。
soap的传输方式
SOAP的传输协议使用的就是HTTP协议,HTTP传输的内容是HTML文本,而soap传输内容就是soap消息。soap请求其实就是http请求,请求消息中包含SOAPAction
字段,则说明是soap消息。
POST /WebServices/WeatherWebService.asmx HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3603)
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://WebXml.com.cn/getSupportCity"
Host: www.webxml.com.cn
Content-Length: 348
Expect: 100-continue
Connection: Keep-Alive
<?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消息的请求内容必须以Envelope做为根节点,Body就是请求内容。
soap协议的特点(http的优势)
- 独立的功能实体,各个实例之间耦合性低。
- 大量数据低频访问,也就是一次请求。
- 基于文本传输,文本内容不包含任何逻辑和数据类型,绕过了不同系统不同兼容性问题。
soap和http的区别
HTTP只负责把数据传送过去,不会管这个数据是XML、HTML、图片、文本文件或者别的什么。而SOAP协议则定义了怎么把一个对象变成XML文本,在远程如何调用等。
WSDL
WSDL用XML的格式描述了WebService有哪些方法、参数类型、访问路径等等。要使用一个WebService肯定首先要获取它的WSDL,WSDL通常是开发环境自己生成。
public class Service : System.Web.Services.WebService
{
public Service () {
}
[WebMethod]
public DateTime HelloWorld(int i)
{
return DateTime.Now;
}
}
转成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>
- types定义了WebService用到的所有数据类型
- message指明了需要用的数据类型。
- portType指出了这个WebService哪些方法可供调用。
- binding 中transport指明传输协议,operation 指明要暴露给外界调用的方法。use属性指定输入输出的编码方式,这里没有指定编码。
- services指定服务的一些信息,主要是指定服务的访问路径。