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/"