Tài liệu Module 3: The Underlying Technologies of Web Service - Pdf 84


Contents
Overview 1
HTTP Fundamentals 2
XML Essentials 14
SOAP Fundamentals 26
Lab 3: Issuing HTTP and SOAP Requests
Using the .NET Framework 39
Review 45

Module 3: The
Underlying
Technologies of Web
Services
Information in this document, including URL and other Internet Web site references, is subject to
change without notice. Unless otherwise noted, the example companies, organizations, products,
domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious,
and no association with any real company, organization, product, domain name, e-mail address,
logo, person, places or events is intended or should be inferred. Complying with all applicable
copyright laws is the responsibility of the user. Without limiting the rights under copyright, no
part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or

Issue HTTP POST and GET requests and process the responses by using
the Microsoft
®
.NET Framework.
!
Describe data types by using the XML Schema Definition language (XSD).
!
Explain how to control the way a .NET Framework object is serialized to
XML.
!
Describe the structures of a Simple Object Access Protocol (SOAP) request
and response.
!
Issue a SOAP request and process the response by using the .NET
Framework.

Materials and Preparation
This section provides the materials and preparation tasks that you need to teach
this module.
Required Materials
To teach this module, you need the Microsoft PowerPoint
®
file 2524A_03.ppt.
Preparation Tasks
To prepare for this module:
!
Read all of the materials for this module.
!
Try the walkthroughs and demonstrations in this module.
!

!
SOAP Fundamentals
This topic is intended to provide students with a basic understanding of the
SOAP protocol and explain how to issue SOAP requests using the .NET
Framework. Emphasize that SOAP is the preferred wire format for Web
Services. Explain to the students that the .NET Framework handles most of
the details of communication using SOAP in Web Services implemented
using the .NET Framework.

Module 3: The Underlying Technologies of Web Services 1 Overview
!
HTTP Fundamentals
!
XML Essentials
!
SOAP Fundamentals

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Web Services are built on Web technologies. The three core technologies that
form the foundation for Web Services are the Hypertext Transfer Protocol
(HTTP), the Extensible Markup Language (XML), and the Simple Object
Access Protocol (SOAP). It is important to understand the workings of the three
technologies and how the Microsoft

In this module, you will learn
about the some of the
technologies underlying
Web Services.
Note
2 Module 3: The Underlying Technologies of Web Services "
""
"

HTTP Fundamentals
!
Overview of HTTP
!
Structures of HTTP Requests and Responses
!
The GET and POST Methods
!
HTTP Using the .NET Framework
!
Code Walkthrough: Issuing a Synchronous HTTP
Request
!
Code Walkthrough: Issuing an Asynchronous HTTP
Request

*****************************
ILLEGAL FOR NON


*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
A resource location is specified in HTTP through a mechanism known as a
Uniform Resource Locator (URL). Strictly speaking, the mechanism used in
HTTP is a Uniform Resource Identifier (URI), but we can also think of it as a
URL.

A URI identifies a document, whereas a URL identifies a document and
its location.

Syntax of a URL
The syntax of a URL is as follows:
http://host[:port][path[?querystring]]

The following is an example of a URL:
http://www.woodgrovebank.com/accts.asp?AccNo=23

In the preceding example, www.woodgrovebank.com is the host, accts.asp is the
path and AccNo=23 is the query string. If the port number is not specified (as in
the preceding example), the default port for HTTP, which is port 80, is used.
Stateless Protocol
HTTP is a stateless protocol. This means that whenever a request is made by
the client, the connection to the server is closed after the response is received
from the server. Therefore, if any state must be maintained between the client
and the server, the server must pass on state information with the response to
the client. This will enable the server to recover this information from the client

Content-Length: 11
Symbol=MSFT
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 75
<?xml version="1.0" encoding="utf-8"?>
<stock symbol="MSFT" Price="71.50" />
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 75
<?xml version="1.0" encoding="utf-8"?>
<stock symbol="MSFT" Price="71.50" />
Note the blank line!

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
HTTP requests and responses have a simple structure.
Structure of an HTTP Request
An HTTP request has the following format:
method URL Version
headers
a blank line
message body

An example code of an HTTP request is as follows:
POST /TheStockExchange/Trading/GetStockPrice.asp HTTP/1.1
Host: localhost

DELETE
!
TRACE
!
CONNECT
!
extension-method In Course 2524A, Developing XML Web Services Using Microsoft Visual
C# .NET Beta 2, you will learn about the GET and POST methods only.

Structure of an HTTP Response
An HTTP response has the following format:
Version Status-Code Description
headers
a blank line
message body

Example
An example code of an HTTP response is as follows:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 75

<?xml version="1.0" encoding="utf-8"?>
<stock symbol="MSFT" Price="71.50" />

Note
S

Service. This is because these methods are designed specifically for submitting
data to a Web server and retrieving a specified resource from a Web server.
This makes it possible to layer a function call model on top of these methods,
which is exactly the model required for Web Services.
HTTP-GET Request
Consider the following HTTP-GET request:
GET /Trading/GetStockPrice.asp?Symbol=MSFT HTTP/1.1
Host: localhost

The most important feature of the request line is the querystring. The
querystring is the portion of the URI that follows the question mark, and
consists of a set of URL-encoded name/value pairs.
In an HTTP-GET request, there is typically no message body. The response for
a GET request is just a standard HTTP response, which is described in the
previous topic.
Topic Objective
To describe GET and POST
methods and explain the
differences between them.
Lead-in
The GET and POST request
methods are ideal for Web
Services communication.
Delivery Tip
While discussing the details
of an HTTP-GET request,
point out to the parts of the
corresponding URL on the
preceding slide, in particular
the query string. Emphasize

that there is typically no
query string. Also, point out
the blank line between the
header and the body.
8 Module 3: The Underlying Technologies of Web Services HTTP Using the .NET Framework
!
HttpWebRequest and HttpWebResponse
!
StreamReader and StreamWriter
!
Support for Synchronous and Asynchronous
Operations

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Issuing an HTTP request and receiving a response is easy using the .NET
Framework. All of the basic functionality that is required is provided by the
following classes in the .NET Framework class library:
!
HttpWebRequest and HttpWebResponse classes in the System.Web
namespace
!
StreamReader and StreamWriter classes in the System.IO namespace


used to read and write streams using a specific encoding (UTF-8/UTF-16, etc.).
Support for Synchronous and Asynchronous Operations
The HttpWebRequest class supports both synchronous and asynchronous
requests. In the next two topics, you will look at code samples of synchronous
and asynchronous operations.
10 Module 3: The Underlying Technologies of Web Services Code Walkthrough: Issuing a Synchronous HTTP Request

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
In this code walkthrough, you will look at how a synchronous HTTP request is
issued using the .NET Framework..
Let us examine the functionality implemented by the following sample code for
a synchronous request: 1. HttpWebRequest req = (HttpWebRequest )!
WebRequest.Create(url);
2. req.ContentType=contentType;
3. req.Method = method;
4. req.ContentLength=content.Length;
5. Stream s;
6. s = req.GetRequestStream();
7. StreamWriter sw = new StreamWriter(s,Encoding.ASCII);
8. sw.Write(content);

18. {
19. nBytes = sr.Read(data,0,(int)1024);
20. sb.Append(data);
21. } while (nBytes == 1024);

The functionality implemented by the preceding code can be described as
follows:
!
In line 1, the return value of the WebRequest.Create call is cast to
HttpWebRequest.
In most cases, the WebRequest and WebResponse classes provide all of
the functionality that you need to perform an HTTP request. However, if
you need to access HTTP-specific features such as HTTP headers, you need
a protocol-specific derived class of WebRequest.
!
In lines 2 through 4, HTTP-specific properties are set.
!
In lines 6 through 9, the content for the request is written to a stream.
Note in line 7 the type of encoding is specified for the stream.
!
In line 11, the response from the server is retrieved.
!
In lines 12 through 22, the content of the response message is read.
Because the response stream is not seekable, the total amount of data to be
read cannot be determined at the start of the content retrieval. This is the
reason for retrieving the content in blocks.

12 Module 3: The Underlying Technologies of Web Services
16. ...
Topic Objective
To walkthrough the code for
a sample asynchronous
HTTP request.
Lead-in
In this code walkthrough,
you will look at how an
asynchronous HTTP
request is issued using the
.NET Framework.
Delivery Tip
To explain the sample code
to the students, open the file
<install
folder>\Democode\Mod03\
HTTP Request
(Asynchronous).txt.
Module 3: The Underlying Technologies of Web Services 13 17. public class Handler
18. {
19. public void Callback(IAsyncResult ar)
20. {
21. // Get the WebRequest from RequestState.
22. HttpWebRequest req = (HttpWebRequest)!
ar.AsyncState;
23. // Get the response object associated
24. // with the request.

In line 12, an instance of a delegate of type AsyncCallback is created, and a
reference to the Callback method of the Handler class is passed to the
constructor of the delegate.
!
In line 14, an asynchronous request is initiated for a response by using the
BeginGetResponse method.
A reference to the delegate and a reference to an object that contains any
state that might be needed by the method that handles the completion of the
request is passed as parameters. In line 14, the request object is passed.
!
In line 20, the Callback function receives a reference to an IAsyncResult
interface as a parameter.
!
In line 26, the asynchronous request is completed.
!
In lines 29 through 38, the response content is retrieved exactly in the way it
is done in a synchronous operation.

14 Module 3: The Underlying Technologies of Web Services "
""
"

XML Essentials
!
Overview of XML
!
XSD Fundamentals

Schemas

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Considering the central role that XML plays in Web Services, it is useful to
review some of its important concepts.
Elements and Attributes
After the document prolog, all XML documents have a root element with child
elements. Any of the elements may have attributes that provide further
information about a particular element. A common source of confusion is when
to use elements vs. when to use attributes. There are no absolute rules for this
choice. However, the following table summarizes and contrasts some of the
most important characteristics of elements and attributes.
Characteristics Elements Attributes

May have child nodes # $
Are ordered # $
May be repeated # $
May be indexed # $
May be typed # #
May have a default value $ #

When describing the data to be consumed or returned by your Web Service, it is
important to keep in mind the differences between elements and attributes to
use them appropriately in your XML documents.
Topic Objective
To explain the important

An attribute cannot be repeated in an element.

Now that you have reviewed some of the important concepts of XML, let us
look at how XML is used in Web Services.
Schemas
To be able to successfully use a Web Service, you need to know the operations
supported by the Web Service and the structure of the documents (or messages)
that are consumed and produced by each operation. This information is defined
in a document, known as a service description, which describes a Web Service.
The service description is created using the Web Service Description Language
(WSDL), which is an XML-based language.
Within the WSDL documents, you define XSD schemas that describe the data
types and document structures allowed in XML documents. XSD schemas
validate XML documents in a mechanical way. This frees the programmer from
the error-prone task of correctly parsing and validating a complex document
structure.
You will learn the basics of XSD later in this module. For more information
about WSDL, see Module 4, “Consuming Web Services,” in Course 2524A,
Developing XML Web Services Using Microsoft Visual C# .NET Beta 2.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status