soap和wsdd的理解

SOAP(简单对象访问协议)是一种基于XML的网络服务协议,利用HTTP进行传输,特点是功能实体间耦合度低,适用于低频且大量数据的访问。与HTTP区别在于SOAP定义了数据转换和远程调用的规范。WSDL是Web Service描述语言,通过XML定义服务的方法、参数、访问路径等,是调用WebService前的必要信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的优势)
  1. 独立的功能实体,各个实例之间耦合性低。
  2. 大量数据低频访问,也就是一次请求。
  3. 基于文本传输,文本内容不包含任何逻辑和数据类型,绕过了不同系统不同兼容性问题。
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>
  1. types定义了WebService用到的所有数据类型
  2. message指明了需要用的数据类型。
  3. portType指出了这个WebService哪些方法可供调用。
  4. binding 中transport指明传输协议,operation 指明要暴露给外界调用的方法。use属性指定输入输出的编码方式,这里没有指定编码。
  5. services指定服务的一些信息,主要是指定服务的访问路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值