SOAP教程[转]

SOAP教程 需要帮助?

SOAP是一个简单的基于XML的协议,它让应用程序跨HTTP进行信息交换。
在我们的SOAP教程,你将了解什么是SOAP,以及它是怎样利用XML来让程序间的信息进行交换的

内容目录

SOAP 介绍
This chapter explains what SOAP is and why SOAP is useful.
介绍了什么是SOAP以及它的用处

SOAP 语法
This chapter describes the building blocks of SOAP and SOAP syntax rules.
这章介绍了如何建立SOAP块结构和SOAP的语法规则

SOAP Envelope
This chapter describes the SOAP Envelope element.
SOAP的封套元素

SOAP Header
This chapter describes the SOAP Header element.
这章介绍了SOAP的Header[报头]元素

SOAP Body
This chapter describes the SOAP Body element.
这章介绍了SOAP的Body元素

SOAP Fault
This chapter describes the SOAP Fault element.
这章介绍了SOAP的默认元素

SOAP HTTP Binding
This chapter is an introduction to the SOAP HTTP binding.
介绍有关SOAP HTTP 绑定

SOAP 举例
This chapter describes a SOAP example.
一个SOAP的举例

SOAP 摘要


SOAP 介绍
w3pop.com / 2006-09-19

SOAP is a simple XML based protocol to let applications exchange information over HTTP.
SOAP是一个简单的基于XML的协议,它让应用程序跨HTTP进行信息交换。

Or more simply: SOAP is a protocol for accessing a Web Service.
或者更简单的:SOAP是用于访问网络服务的协议。


What You Should Already Know
你应该已经了解的:

Before you study SOAP you should have a basic understanding of XML and XML Namespaces.
在你学习SOAP 之前,你应该能基本理解以下XML和XML命名空间。

If you want to study these subjects first, please read our XML Tutorial.
如果你想继续看下去,请先访问XML 教程.


What is SOAP?
什么是SOAP?

  • SOAP stands for Simple Object Access Protocol
    SOAP代表Simple Object Access Protocol(简单对象访问协议)
  • SOAP is a communication protocol
     SOAP是通讯协议
  • SOAP is for communication between applications
    SOAP用于应用程序之间的通讯
  • SOAP is a format for sending messages
    SOAP是一种信息发送的格式
  • SOAP is designed to communicate via Internet
    SOAP是通过因特网来通讯的
  • SOAP is platform independent
    SOAP是独立的平台
  • SOAP is language independent
    SOAP是独立的语言
  • SOAP is based on XML
    SOAP是建立在XML基础之上的
  • SOAP is simple and extensible
    SOAP是简单并可以扩展的
  • SOAP allows you to get around firewalls
    SOAP允许透过防火墙传播
  • SOAP will be developed as a W3C standard
    SOAP会以W3C标准为标准而发展起来。

Why SOAP?
为什么使用SOAP?

It is important for application development to allow Internet communication between programs.
对应用程序而言,允许程序之间的因特网通讯是非常重要的。

Today's applications communicate using Remote Procedure Calls (RPC) between objects like DCOM and CORBA, but HTTP was not designed for this. RPC represents a compatibility and security problem; firewalls and proxy servers will normally block this kind of traffic.
如今的应用软件都是通过对象之间的RPC(远程过程调用,如:DCOM和CORBA)来实现通讯的,但是HTTP却不是这样的。RPC表现为兼容性和安全问题。一般情况下,防火墙和代理服务器会阻塞流量。

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.
因为所有的因特网浏览器都支持HTTP,所以在应用程序之间进行通讯还有一个更好的办法,就是跨HTTP。SOAP就是基于这个理论而被创建出来的。

SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.
SOAP提供了一条途径使在不同操作系统上运行的应用程序间实现通讯(通过不同技术和程序设计语言)。


Microsoft and SOAP
微软和SOAP

SOAP is a key element of Microsoft's .NET architecture for future Internet application development.
在未来因特网应用程序发展进程中,SOAP是微软.NET体系结构的主要元素。


SOAP 1.1 was Proposed to W3C
向W3C提议SOAP1.1

UserLand, Ariba, Commerce One, Compaq, Developmentor, HP, IBM, IONA, Lotus, Microsoft, and SAP proposed to W3C, in May 2000, the SOAP Internet protocol that they hope will revolutionize application development by connecting graphic user interface desktop applications to powerful Internet servers using the standards of the Internet: HTTP and XML.
在2000年5月,UserLand, Ariba, Commerce One, Compaq, Developmentor, HP, IBM, IONA, Lotus, Microsoft和 SAP对W3C做出提议:他们所设想的SOAP英特网协议将通过使用HTTP以及XML两个网络标准把图形用户界面桌面应用程序(graphic user interface desktop applications)连接到强大的英特网服务中。


W3C is Working on SOAP 1.2
W3C将在SOAP 1.2上运行

The first public Working Draft on SOAP was published from W3C in December 2001. To read more about the SOAP activities at W3C please visit our W3C tutorial.
2001年12月,SOAP上的第一份公共工作草案将在W3C上公布。如想了解更多SOAP在W3C上的活动,请访问W3C 教程。

SOAP 语法
w3pop.com / 2006-09-21

SOAP Building Blocks
SOAP组件群

A SOAP message is an ordinary XML document containing the following elements:
Soap信息是包含以下元素的普通XML文件。

  • A required Envelope element that identifies the XML document as a SOAP message
    必需的把XML文件定义为SOAP信息的封套(Envelope)元素
  • An optional Header element that contains header information
    包含标题信息的可选标题(header)元素
  • A required Body element that contains call and response information
    包含请求与响应信息的必要主体元素
  • An optional Fault element that provides information about errors that occurred while processing the message
    处理信息时,提供关于产生错误信息的可选择的错误元素

All the elements above are declared in the default namespace for the SOAP envelope:
上面所有元素都在SOAP 封套(envelope)的错误命名空间中被声明

http://www.w3.org/2001/12/soap-envelope

and the default namespace for SOAP encoding and data types is:
SOAP编码和数据类型的的错误命名空间是:

http://www.w3.org/2001/12/soap-encoding

Syntax Rules
语法规则

Here are some important syntax rules:
下面是一些重要的语法规则:

  • A SOAP message MUST be encoded using XML
    SOAP信息必须通过XML进行编码
  • A SOAP message MUST use the SOAP Envelope namespace
    SOAP信息必须使用SOAP 封套(Envelope)命名空间(namespace)
  • A SOAP message MUST use the SOAP Encoding namespace
    SOAP 信息必须使用SOAP编码命名空间
  • A SOAP message must NOT contain a DTD reference
    SOAP信息决不能包含DTD参数
  • A SOAP message must NOT contain XML Processing Instructions
    SOAP信息决不能包含XML处理指令

Skeleton SOAP Message
SOAP纲要(Skeleton)信息

<?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>
...
...
</soap:Header>
<soap:Body>
...
...
<soap:Fault>
...
...
</soap:Fault>
</soap:Body>
</soap:Envelope>

SOAP 封套
w3pop.com / 2006-09-21

The mandatory SOAP Envelope element is the root element of a SOAP message.
具有强制性的SOAP封套(Envelope)元素是SOAP信息的根元素。


The SOAP Envelope Element
SOAP封套元素

The required SOAP Envelope element is the root element of a SOAP message. It defines the XML document as a SOAP message.
必要的SOAP封套元素是SOAP元素的根元素。它把XML文件定义为SOAP信息。

Note the use of the xmlns:soap namespace. It should always have the value of:
注意xmlns:soap命名空间(namespace)的使用。它一般含有以下值:

http://www.w3.org/2001/12/soap-envelope

and it defines the Envelope as a SOAP Envelope:
它以SOAP 封套(Envelope)的形式定义了封套(Envelope)。

<?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">
...
Message information goes here
...
</soap:Envelope>


The xmlns:soap Namespace
xmlns:soap命名空间

A SOAP message must always have an Envelope element associated with the "http://www.w3.org/2001/12/soap-envelope" namespace.
SOAP信息必须含有与"http://www.w3.org/2001/12/soap-envelope" 命名空间相关的Envelope元素。

If a different namespace is used, the application must generate an error and discard the message.
如果使用了不同的命名空间,那应用程序肯定会产生一个错误信息并忽略信息。


The encodingStyle Attribute
encodingStyle属性

The SOAP encodingStyle attribute is used to define the data types used in the document. This attribute may appear on any SOAP element, and it will apply to that element's contents and all child elements. A SOAP message has no default encoding.
SOAPencodingStyle属性的作用:定义用于文档中的数据类型。属性可能会在任何SOAP元素中出现,并且会应用于元素的内容及其所有子元素上。SOAP信息没有默认编码。

Syntax
语法
soap:encodingStyle="URI"
Example
实例
<?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">
...
Message information goes here
...
</soap:Envelope>

SOAP Header
w3pop.com / 2006-09-21

The optional SOAP Header element contains header information.
可选择的SOAP标题元素包含了标题信息。


The SOAP Header Element
SOAP标题元素

The optional SOAP Header element contains application specific information (like authentication, payment, etc) about the SOAP message. If the Header element is present, it must be the first child element of the Envelope element.
可选择的SOAP标题元素包含了关于SOAP信息的应用程序的详细信息(如:验证(authentication)、返还值(payment)等)。如果标题元素是存在的,那它必须是Envelope元素的第一个子元素。

Note: All immediate child elements of the Header element must be namespace-qualified.
注意:标题(Header)元素的所有紧接着的子元素必须是有命名空间资格(namespace-qualified)的。

<?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:Envelope>

The example above contains a header with a "Trans" element, a "mustUnderstand" attribute value of "1", and a value of 234.
上面的例子包含了含有"Trans"元素的标题,属性值为1的"mustUnderstand"以及一个234值。

SOAP defines three attributes in the default namespace ("http://www.w3.org/2001/12/soap-envelope"). These attributes are: actor, mustUnderstand, and encodingStyle. The attributes defined in the SOAP Header defines how a recipient should process the SOAP message.
SOAP定义了3个默认空间("http://www.w3.org/2001/12/soap-envelope")的属性。这些属性是:actor, mustUnderstand,和 encodingStyle。定义在SOAP中的这些属性定义了容器如何处理SOAP信息。


The actor Attribute
行为(actor)属性

A SOAP message may travel from a sender to a receiver by passing different endpoints along the message path. Not all parts of the SOAP message may be intended for the ultimate endpoint of the SOAP message but, instead, may be intended for one or more of the endpoints on the message path.
SOAP信息会通过不同的端点沿着信息路径从发送人传到接收者。不是所有SOAP部分都会用于SOAP信息的最终端点,但是,作为交换,在信息路径中它们可能用于一个或更多端点。

The SOAP actor attribute may be used to address the Header element to a particular endpoint.
SOAP行为属性可能用于特定端点的标题元素寻址。

Syntax
语法
soap:actor="URI"
Example
实例
<?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:actor="http://www.w3schools.com/appml/">
234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>


The mustUnderstand Attribute
mustUnderstand属性

The SOAP mustUnderstand attribute can be used to indicate whether a header entry is mandatory or optional for the recipient to process.
Soap mustUnderstand属性可以用来指明标题入口(header entry)对于接受者信息处理是强制的还是可选的。

If you add "mustUnderstand="1" to a child element of the Header element it indicates that the receiver processing the Header must recognize the element. If the receiver does not recognize the element it must fail when processing the Header.
如果你把"mustUnderstand="1"添加到一个子标题元素的元素上去的话,那就意味着接受者处理标题(Header)元素时,这个(标题)元素必须是可识别的;如果接收器无法识别该元素,那么处理标题元素时将会失败。

Syntax
语法
soap:mustUnderstand="0|1"
Example
实例
<?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:Envelope>


The encodingStyle Attribute
encodingStyle属性

The SOAP encodingStyle attribute is explained in the previous chapter.
SOAP encodingStyle属性在前一章中已得以阐述。

SOAP Body
w3pop.com / 2006-09-21

The mandatory SOAP Body element contains the actual SOAP message.
具有强制性的SOAP主体元素包含了现行的SOAP信息。


The SOAP Body Element
Soap主体元素

The required SOAP Body element contains the actual SOAP message intended for the ultimate endpoint of the message.
所要求的SOAP主体元素包含了用于信息终端的现行SOAP信息。

Immediate child elements of the SOAP Body element may be namespace-qualified. SOAP defines one element inside the Body element in the default namespace ("http://www.w3.org/2001/12/soap-envelope"). This is the SOAP Fault element, which is used to indicate error messages.
直接的SOAP主体元素的子元素可能是有资格获得命名空间(name-space)的。SOAP以默认的命名空间定义了("http://www.w3.org/2001/12/soap-envelope")主体元素内的一个元素。这是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:Body>
   <m:GetPrice xmlns:m="http://www.w3schools.com/prices">
      <m:Item>Apples</m:Item>
   </m:GetPrice>
</soap:Body>
</soap:Envelope>

The example above requests the price of apples. Note that the m:GetPrice and the Item elements above are application-specific elements. They are not a part of the SOAP standard.
上面举的是请求苹果价格的例子。注意,上面的m:GetPrice和项目元素都是详细的应用程序(application-specific)元素。它们不是SOAP标准的一部分。

A SOAP response could look something like this:
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:Body>
   <m:GetPriceResponse xmlns:m="http://www.w3schools.com/prices">
      <m:Price>1.90</m:Price>
   </m:GetPriceResponse>

</soap:Body>
</soap:Envelope>

SOAP Fault
w3pop.com / 2006-09-21

The optional SOAP Fault element is used to hold error and status information for a SOAP message.
可选择的SOAP错误元素的作用是:保存SOAP信息中的错误信息和状态信息。


The SOAP Fault Element
SOAP错误信息

An error message from a SOAP message is carried inside a Fault element.
SOAP信息中包含的错误信息位于“错误元素”中。

If a Fault element is present, it must appear as a child element of the Body element. A Fault element can only appear once in a SOAP message.
如果错误元素是存在的,那它肯定以主体元素的子元素形式出现。错误元素只能在SOAP元素中出现一次。

The SOAP Fault element has the following sub elements:
SOAP错误元素含有以下子元素:

Sub Element
子元素
Description
描述
<faultcode>A code for identifying the fault
鉴别错误信息的代码
<faultstring>A human readable explanation of the fault
将错误信息解释为可以被人们读懂的信息
<faultactor>Information about who caused the fault to happen
引发错误产生的信息
<detail>

Holds application specific error information related to the Body element
解释了与主体元素相关的应用程序中详细的错误信息

SOAP Fault Codes
SOAP错误代码

The faultcode values defined below must be used in the faultcode element when describing faults:
以下定义的错误代码值必须是用于错误代码元素(描述错误时):

Error
错误
Description
描述
VersionMismatchFound an invalid namespace for the SOAP Envelope element
为SOAP包络(Envelope)元素找出一个无效的命名空间
MustUnderstandAn immediate child element of the Header element, with the mustUnderstand attribute set to "1", was not understood
如果Header 元素中的直接子元素的“mustUnderstand” 设置为1,那表示这个子元素不可识别。
ClientThe message was incorrectly formed or contained incorrect information
信息格式错误或包含了错误信息
ServerThere was a problem with the server so the message could not proceed
由于服务器的错误而导致信息不能被处理

SOAP HTTP Binding
w3pop.com / 2006-09-21

The HTTP Protocol

HTTP communicates over TCP/IP. An HTTP client connects to an HTTP server using TCP. After establishing a connection, the client can send an HTTP request message to the server:
HTTP是通过TCP/IP进行通讯的。 HTTP客户端通过TCP协议连接到HTTP服务器。在建立连接之后,客户端就可以向服务器发送HTTP请求信息了。

POST /item HTTP/1.1
Host: 189.123.345.239
Content-Type: text/plain
Content-Length: 200

The server then processes the request and sends an HTTP response back to the client. The response contains a status code that indicates the status of the request:
服务器会处理请求并通过HTTP来响应客户端。响应包含有指明请求者状态的状态代码。

200 OK
Content-Type: text/plain
Content-Length: 200

In the example above, the server returned a status code of 200. This is the standard success code for HTTP.
在下面这个例子中,服务器会返回一个200的状态代码。这是HTTP的标准成功代码。

If the server could not decode the request, it could have returned something like this:
如果服务器不能破解请求代码,那它就会如下返回:

400 Bad Request
Content-Length: 0


SOAP HTTP Binding
SOAP HTTP捆绑

A SOAP method is an HTTP request/response that complies with the SOAP encoding rules.
SOAP方式是一个遵照SOAP编码规则的HTTP请求/响应。

HTTP + XML = SOAP

A SOAP request could be an HTTP POST or an HTTP GET request.
SOAP请求是HTTP POST 或 HTTP GET请求。

The HTTP POST request specifies at least two HTTP headers: Content-Type and Content-Length.
HTTP POST请求详细说明了最少2个HTTP标题(HTTP header) :Content-Type (内容类型)和 Content-Length.(内容长度)


Content-Type
内容类型

The Content-Type header for a SOAP request and response defines the MIME type for the message and the character encoding (optional) used for the XML body of the request or response.
SOAP请求和响应的内容长度标题定义了用于请求或响应的XML主要部分的信息和字符编码(可选择的)的MIME类型 。

Syntax
语法
Content-Type: MIMEType; charset=character-encoding
Example
实例
POST /item HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8


Content-Length
内容长度

The Content-Length header for a SOAP request and response specifies the number of bytes in the body of the request or response.
SOAP请求和响应的内容长度标题详细说明了请求和响应的主体部分的字节数。

Syntax
语法
Content-Length: bytes
Example
实例
POST /item HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 250

SOAP 实例
w3pop.com / 2006-09-21

A SOAP Example
Soap实例

In the example below, a GetStockPrice request is sent to a server. The request has a StockName parameter, and a Price parameter will be returned in the response. The namespace for the function is defined in "http://www.example.org/stock" address.
在下面的实例中会发送GetStockPrice请求给服务器。请求中包含一个StockName参数,价格参数会在响应中返回。函数的命名空间(namespace)在http://www.example.org/stock中有详细说明。

The SOAP request:
SOAP请求:

POST /InStock HTTP/1.1
Host: www.example.org
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:GetStockPrice>
<m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
</soap:Body>
</soap:Envelope>

A SOAP response:
SOAP响应:

HTTP/1.1 200 OK
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>

SOAP 摘要
w3pop.com / 2006-09-21

SOAP Summary
SOAP 概要

This tutorial has taught you how to use SOAP to exchange information between applications over HTTP.
此教程说明的是如何通过SOAP在跨HTTP的应用程序之间进行信息转换。

You have learned about the different elements and attributes in a SOAP message.
你已经在一条SOAP信息中了解了在SOAP信息中的不同元素与属性。

You have also learned how use SOAP as a protocol for accessing a web service.
你也了解了如何把SOAP当作一份访问网络服务的协议来使用。


Now You Know SOAP, What's Next?
你已经了解了SOAP,那接下来就要了解:

The next step is to learn about WSDL and Web Services.
下一步你需要学习WSDL和网络服务内容:

WSDL

WSDL is an XML-based language for describing Web services and how to access them.
WSDL是以一种基于XML的语言。它是用于描述网络服务和如何对他们进行访问的一种语言。

WSDL describes a web service, along with the message format and protocol details for the web service.
WSDL是根据网络服务的信息格式以及协议细节对一个网络服务进行描述的。

If you want to learn more about WSDL, please visit our WSDL tutorial.
如想了解更多,请访问WSDL 教程.

Web Services

Web services can convert your applications into web-applications.
网络服务可以把应用程序转换成网络应用程序。

By using XML,  messages can be sent between applications.
通过XML ,信息可以在应用程序之间互发。

If you want to learn more about web services, please visit our Web Services tutorial.
如想了解更多,请访问Web Services 教程.

转载于:https://www.cnblogs.com/caoxch/archive/2006/11/17/563706.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值