1 soap simple object access protocol 大葉大學資工系. 2 purpose of soap developers need to...

30
1 SOAP Simple Object Access Protocol 大大大大大大大

Upload: leona-byrd

Post on 24-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

1

SOAPSimple Object Access Protocol

大葉大學資工系

Page 2: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

2

Purpose of SOAP

Developers need to establish a standard transport and data-exchange framework to achieve XML-based interaction.

The reasons for using SOAP

• Web services require a protocol that uses a standard, open data format.

• Web services need a way to work with the basic security mechanisms of remote networks.

Page 3: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

3

Nature of SOAP

SOAP is a software system that enables applications to communicate with one another using XML-based message, called SOAP message.

SOAP messages encapsulate the information exchanged.

• SOAP messages don’t provide programming instructions, rather, specify the operations to invoke.

SOAP supports features such as attachments, security, routing information and transaction.

Page 4: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

4

Flow of a SOAP Request

Client-Side

ConsumerSOAP

MessageProxySerialize

Deserialize

Server-SideIIS

ASPNET_ISAPI.DLL

HTTP Handlers

HTTP Web Service HandlerWeb

ServiceSerialize

Deserialize

NetworkNetwork

Page 5: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

5

SOAP Specifications

SOAP envelope describes the format of a SOAP message.

The second part defines a set of rules that encode data types.

The third part defines how a SOAP message can execute remote procedure call (RPC).

The last part specifies the message binding protocol.

• HTTP, HTTPS, SMTP

Page 6: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

6

Anatomy of a SOAP Message

A SOAP message is composed of an envelope that contains the body of the message and any header information used to describe the message.

The root element of the document is the Envelope element.

• The envelope can contain an optional Header element, which contains information about the message.

• The envelope must contain one Body element.

Page 7: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

7

A SOAP Envelop

A SOAP Envelop

Header

Body

Page 8: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

8

SOAP Actors

A SOAP actor is anything that acts on the content of the SOAP message.

There are two types of SOAP actors:

• The default actor is the intended final recipient of a SOAP message.

• An intermediary receives a SOAP message and might act on the message before forwarding it along the intended message path.

Page 9: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

9

Header Element

The optional Header element is used to pass data that might not be appropriate to encode in the body.

• Authentication, Security digest information, Routing information, Transactions, Payment information.

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <Digest>B839D234A3F87</Digest> </soap:Header> <soap:Body> <StockReport> <Symbol>MSFT</Symbol> <Price>74.56</Price> </StockReport> </soap:Body></soap:Envelope>

Page 10: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

10

Body Element

The body contains the payload of the message and there are no restrictions on how the body can be encoded.

SOAP messages are placed into two categories:

• Procedure-oriented messages provide two-way communication and are commonly referred to as remote procedure call (RPC) messages.

• Document-oriented messages generally facilitate one-way communication. Business documents such as purchase orders are examples of document-oriented messages.

Page 11: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

11

SOAP 1.1 XML Schema Definition

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" targetNamespace="http://schemas.xmlsoap.org/soap/envelope/" > <!-- Envelope, header and body --> <xs:element name="Envelope" type="tns:Envelope" /> <xs:complexType name="Envelope" > <xs:sequence> <xs:element ref="tns:Header" minOccurs="0" /> <xs:element ref="tns:Body" minOccurs="1" /> <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" /> </xs:sequence> <xs:anyAttribute namespace="##other" processContents="lax" /> </xs:complexType>

Page 12: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

12

SOAP 1.1 XML Schema Definition

<xs:element name="Header" type="tns:Header" /> <xs:complexType name="Header" > <xs:sequence> <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" /> </xs:sequence> <xs:anyAttribute namespace="##other" processContents="lax" /> </xs:complexType> <xs:element name="Body" type="tns:Body" /> <xs:complexType name="Body" > <xs:sequence> <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" /> </xs:sequence> <xs:anyAttribute namespace="##any" processContents="lax" /> </xs:complexType>

Page 13: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

13

SOAP 1.1 XML Schema Definition

<!-- Global Attributes --> <xs:attribute name="mustUnderstand" default="0" > <xs:simpleType> <xs:restriction base='xs:boolean'> <xs:pattern value='0|1' /> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="actor" type="xs:anyURI" />

Page 14: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

14

SOAP 1.1 XML Schema Definition

<xs:simpleType name="encodingStyle" > <xs:list itemType="xs:anyURI" /> </xs:simpleType>

<xs:attribute name="encodingStyle" type="tns:encodingStyle" /> <xs:attributeGroup name="encodingStyle" > <xs:attribute ref="tns:encodingStyle" /> </xs:attributeGroup>

<xs:element name="Fault" type="tns:Fault" /> <xs:complexType name="Fault" final="extension" > <xs:sequence> <xs:element name="faultcode" type="xs:QName" /> <xs:element name="faultstring" type="xs:string" /> <xs:element name="faultactor" type="xs:anyURI" minOccurs="0" /> <xs:element name="detail" type="tns:detail" minOccurs="0" /> </xs:sequence> </xs:complexType>

Page 15: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

15

SOAP 1.1 XML Schema Definition

<xs:complexType name="detail"> <xs:sequence> <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" /> </xs:sequence> <xs:anyAttribute namespace="##any" processContents="lax" /> </xs:complexType>

</xs:schema>

Page 16: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

16

SOAP Message Example

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <x:TransferFunds xmlns:x="urn:examples-org:banking"> <from>22-342439</from> <to>98-283843</to> <amount>100.00</amount> </x:TransferFunds> </soap:Body></soap:Envelope>

Page 17: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

17

SOAP Message Example

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <x:TransferFundsResponse xmlns:x="urn:examples-org:banking"> <balances> <account> <id>22-342439</id> <balance>33.45</balance> </account> <account> <id>98-283843</id> <balance>932.73</balance> </account> </balances> </x:TransferFundsResponse> </soap:Body></soap:Envelope>

Page 18: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

18

SOAP Message Example

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <soap:Fault> <faultcode>soap:Server</faultcode> <faultstring>Insufficient funds</faultstring> <detail> <x:TransferError xmlns:x="urn:examples-org:banking"> <sourceAccount>22-342439</sourceAccount> <transferAmount>100.00</transferAmount> <currentBalance>89.23</currentBalance> </x:TransferError> </detail> </ soap:Fault > </soap:Body></soap:Envelope>

Page 19: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

19

Procedure-oriented Messages

Two SOAP messages are paired together to facilitate an RPC method call with SOAP:

• the request message

• Information about the targeted method along with any input parameters is passed to the server via a request message.

• the corresponding response message

• The server invokes some behavior (or procedure) and returns the results and any return parameters.

Page 20: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

20

Document-oriented Messages

A business document such as a purchase order or an invoice can be encoded within the body of a SOAP message and routed to its intended recipient.

The recipient of the document might or might not send an acknowledgment message back to the sender.

Page 21: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

21

SOAP RPC Messages

Remote Procedure Call (RPC) is a technology by which one application invokes a procedure residing on another computer.

The SOAP message body contains the Web service method to be invoked, any parameters the method takes and the target procedure’s URI.

Page 22: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

22

SOAP Example

Add(1, 2)

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Add> <x>1</x> <y>2</y> </Add> </soap:Body></soap:Envelope>

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Add> <x>1</x> <y>2</y> </Add> </soap:Body></soap:Envelope>

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <AddResult> <result>3</result> </AddResult> </soap:Body></soap:Envelope>

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <AddResult> <result>3</result> </AddResult> </soap:Body></soap:Envelope>

public int Add(int x, int y){ return x + y;}

Page 23: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

23

SOAP Encoding

SOAP Encoding defines the way data can be serialized within a SOAP message.

SOAP Encoding builds on the types defined in the XML specification.

Simple Types

• An instance of a data type is encoded as an XML element.

• integer called Age would be encoded as<Age>31</Age>

Page 24: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

24

SOAP Encoding

Compound Type – Structures

• Each of the variables contained within the instance of the structure is serialized as a child element of the parameter element.

public struct RectSolid{ public int length; public int width; public int height;}

public int CalcVolume(RectSolid r){ return (r.length * r.width * r.height);}

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <CalcVolume> <r> <length>2</length> <width>3</width> <height>1</height> </r> </CalcVolume> </soap:Body></soap:Envelope>

Page 25: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

25

SOAP Encoding

Compound Type – Array

• SOAP 1.1 specifies the encoding of Array type.public int AddArray(int[] numbers){ ...}

int[] a = {1, 2, 3};int total;

total = AddArray(a);

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Body> <AddArray> <a soap-enc:arrayType="xsi:int[3]"> <int>1</int> <int>2</int> <int>3</int> </a> </AddArray> </soap:Body></soap:Envelope>

Page 26: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

26

SOAP Encoding

string[][] teams = new string[3][];

teams[0] = new string[3];teams[0][0] = "Bob";teams[0][1] = "Sue";teams[0][2] = "Mike";

teams[1] = new string[2];teams[1][0] = "Jane";teams[1][1] = "Mark";

teams[2] = new String[4];teams[2][0] = "Mary";teams[2][1] = "Jill";teams[2][2] = "Jim";teams[2][3] = "Tom";

RegisterTeams(teams);

public void RegisterTeams(string[][] teams){ // ...} <?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Body> <RegisterTeams> <teams soap-enc:arrayType="xsi:string[3]"> <team soap-enc:arrayType="xsi:string[3]"> <player>Bob</player> <player>Sue</player> <player>Mike</player> </team> <team soap-enc:arrayType="xsi:string[2]"> <player>Jane</player> <player>Mark</player> </team> <team soap-enc:arrayType="xsi:string[4]"> <player>Mary</player> <player>Jill</player> <player>Jim</player> <player>Tom</player> </team> </teams> </RegisterTeams> </soap:Body></soap:Envelope>

Page 27: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

27

Passing Parameters by Reference

Fibonacci series

public void FibonacciIncrement(ref int n1, ref int n2){ int temp = n2;

n1 += n2; n2 = temp + n1;}

int x = 1;int y = 1;

for(int i = 1, i < 11, i += 2){ Console.Write("{0}, {1}", x, y); FibonacciIncrement(x, y);}

<!-- Request Message --><?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <FibonacciIncrement> <n1>1</n1> <n2>1</n2> </FibonacciIncrement> </soap:Body></soap:Envelope>

<!-- Request Message --><?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <FibonacciIncrement> <n1>1</n1> <n2>1</n2> </FibonacciIncrement> </soap:Body></soap:Envelope>

<!-- Request Message --><?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <FibonacciIncrementResponse> <n1>2</n1> <n2>3</n2> </FibonacciIncrementResponse> </soap:Body></soap:Envelope>

<!-- Request Message --><?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <FibonacciIncrementResponse> <n1>2</n1> <n2>3</n2> </FibonacciIncrementResponse> </soap:Body></soap:Envelope>

Page 28: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

28

Protocol Binding

SOAP messages can be sent over any transport protocol that is capable of carrying XML.

The SOAP specification describes only one protocol binding: sending SOAP messages via HTTP POST.

• It is firewall friendly.

• It has a robust supporting infrastructure.

• It is inherently stateless.

• It is simple.

• It maps nicely to RPC-style message exchanges.

• It is open.

Page 29: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

29

Protocol Binding

Page 30: 1 SOAP Simple Object Access Protocol 大葉大學資工系. 2 Purpose of SOAP Developers need to establish a standard transport and data-exchange framework to achieve

30

HTTP Request

Header: contains information about the request and about the client that sent the request.

Body: follows the header and is delimited by two carriage-return/linefeed pairs.

POST /SomeWebService HTTP/1.1 Content-Type: text/xml SOAPAction: "http://somedomain.com/SomeWebService.wsdl" Content-Length: 243 Host: sshort3

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body>

<Add> <x>2</x> <y>2</y>

</Add> </soap:Body>

</soap:Envelope>