Issue HTTP POST and GET requests and process the responses by using the Microsoft® .NET Framework!. HTTP Fundamentals This section is intended to provide students with a basic understan
Trang 1Lab 3: Issuing HTTP and SOAP Requests
Review 45
Module 3: The Underlying
Technologies of Web Services
Trang 2Information 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 otherwise), or for any purpose, without the express written permission of Microsoft Corporation Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property
2001 Microsoft Corporation All rights reserved
Microsoft, MS-DOS, Windows, Windows NT, Active Directory, Authenticode, Biztalk, Intellisense, Jscript, MSDN, PowerPoint, Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, and Windows Media are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries
The names of actual companies and products mentioned herein may be the trademarks of their respective owners
Trang 3Instructor Notes
This module provides students with an overview of the technologies that form the foundation of Extensible Markup Language (XML)-based Web Services After completing this module, students will be able to:
! Describe the structures of a Hypertext Transfer Protocol (HTTP) request and response
! 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
! Complete the lab
Presentation:
120 Minutes
Lab:
45 Minutes
Trang 4Module Strategy
This module is intended to demystify the technologies underlying Web Services Throughout this module, you should emphasize the simplicity of the technologies covered to the students
Use the following strategy to present this module:
! HTTP Fundamentals This section is intended to provide students with a basic understanding of the HTTP protocol and explain how to issue HTTP requests using the NET Framework Explain that HTTP is a simple protocol designed for
interoperability and not performance Emphasize how simple HTTP is to understand
! XML Essentials Explain that XML is fundamental to Web Services Do not spend much time
on the basics of XML Briefly review the important XML concepts Cover the topics on XSD as a progressive tutorial, rather than a list of concepts Explain how the default serialization behavior for NET Framework data types can be modified Explain the importance of the ability to modify default serialization behavior of data types
! 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
Trang 5Overview
! 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® NET Framework provides support for these three technologies to be able to use them in Web Services
Because you must be already familiar with the basics of XML, this module provides a refresher of only the XML concepts that are necessary for implementing and using Web Services
After completing this module, you will be able to:
! Describe the structures of an HTTP request and response
! 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 SOAP request and response
! Issue a SOAP request and process the response by using the NET Framework
In this module, you will learn
about the some of the
technologies underlying
Web Services
Note
Trang 6" 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 - TRAINER USE ******************************
The Hypertext Transfer Protocol (HTTP) is a World Wide Web Consortium (W3C) standard for transferring documents on the Internet Web Services can use HTTP for communication In this section, you will learn the fundamentals
of HTTP and how to make HTTP requests using the NET Framework
and how to make HTTP
requests using the NET
Framework
Trang 7***************************** 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
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 when it receives the next request For example, if you implement a Web site that displays user-specific content, you would have to implement a mechanism that retains information about the current user in order to display personalized content
Trang 8Structures of HTTP Requests and Responses
! Requests
! Responses
POST /TheStockExchange/Trading/GetStockPrice.asp HTTP/1.1 Host: localhost
Content-Type: application/x-www-form-urlencoded Content-Length: 11
Symbol=MSFT
POST /TheStockExchange/Trading/GetStockPrice.asp HTTP/1.1 Host: localhost
Content-Type: application/x-www-form-urlencoded 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
Content-Type: application/x-www-form-urlencoded Content-Length: 11
Trang 9The first line in an HTTP request is known as the request line, and the methods that are supported by a request are as follows:
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
Syntax
Trang 10The GET and POST Methods
! HTTP-GET
! HTTP-POST
GET /Trading/GetStockPrice.asp?Symbol=MSFT HTTP/1.1Host: localhost
GET /Trading/GetStockPrice.asp?Symbol=MSFT HTTP/1.1Host: localhost
POST /Trading/GetStockPrice.asp HTTP/1.1Host: localhost
Content-Type: application/x-www-form-urlencodedContent-Length: 11
Symbol=MSFT
POST /Trading/GetStockPrice.asp HTTP/1.1Host: localhost
Content-Type: application/x-www-form-urlencodedContent-Length: 11
Symbol=MSFT
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
The GET and POST request methods are ideal for communicating with a Web
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 the body is empty
Trang 11HTTP-POST Request
Consider the following HTTP-POST request:
POST /Trading/GetStockPrice.asp HTTP/1.1 Host: localhost
Content-Type: application/x-www-form-urlencoded Content-Length: 11
Symbol=MSFT
In the preceding code, notice that there is no querystring as part of the URI This is because the information about the request is contained in the message body This feature of HTTP-POST makes it a very convenient way of passing larger sets of data to the server in contrast to an HTTP-GET where the size of the querystring is restricted to 1024 bytes Also, passing the data as part of the message body imposes less restrictions on the kind of data that is sent to the server
In Module 5, “Implementing a Simple Web Service,” in Course 2524A,
Developing XML Web Services Using Microsoft Visual C# NET Beta 2, you
will see how the choice of an HTTP request method affects the kinds of interfaces the Web Services can expose
Delivery Tip
While discussing the details
of an HTTP-POST request
point out to the parts of the
corresponding URL on the
preceding slide Emphasize
that there is typically no
query string Also, point out
the blank line between the
header and the body
Trang 12HTTP 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
The NET Framework is a new computing platform designed to simplify application development in the highly distributed environment of the Internet The NET Framework has two main components: the common language runtime and the NET Framework class library
HttpWebRequest and HttpWebResponse WebRequest and WebResponse are abstract base classes in the NET
Framework for accessing data from the Internet in a protocol-independent way
The HttpWebRequest and HttpWebResponse classes, which are derived from WebRequest and WebResponse respectively, encapsulate the HTTP-specific
aspects of the communications with a Web server Most importantly, they provide easy access to the HTTP headers, and the underlying request and response streams
Topic Objective
To explain how to use the
.NET Framework to issue
GET and
HTTP-POST requests
Lead-in
Issuing an HTTP request
and receiving a response is
easy using the NET
Framework
Note
Trang 13StreamReader and StreamWriter The StreamReader and StreamWriter classes are two utility classes that are
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
Trang 14Code 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
In this code walkthrough,
you will look at how a
synchronous 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 (Synchronous)
.txt
Trang 1513 StreamReader sr = new StreamReader(s,Encoding.ASCII);
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
Trang 16Code Walkthrough: Issuing an Asynchronous HTTP Request
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
In this code walkthrough, you will look at how an asynchronous HTTP request
is issued using the NET Framework
Let us examine the functionality implemented by the following sample code for
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
Trang 1717 public class Handler
ar.AsyncState;
26 // Start reading data from the response stream
28
29 StreamReader sr = new StreamReader(s,Encoding.ASCII);
! In line 11, an instance of a custom class named Handler is created
This class will be used to handle the asynchronous completion of the HTTP 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
Trang 18" XML Essentials
! Overview of XML
! XSD Fundamentals
! XML Serialization in the NET Framework
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
In the context of Web Services, XML is used to describe the Web Service interfaces The interface descriptions for a Web Service include definitions of the datatypes for each operation and the format of the messages that are exchanged between a Web Service consumer and a Web Service
Topic Objective
To introduce the topics in
this section
Lead-in
In this section, you will
review some of the
important concepts of XML
that are relevant to Web
Services
Trang 19Overview of XML
! Elements and Attributes
! Well-Formed Documents
! 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
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
features of XML
Lead-in
Considering the central role
that XML plays in Web
Services it is useful to
review some of its important
concepts
Trang 20Well-Formed Documents
All XML documents must be well formed For a document to be well formed, it must adhere to the following rules:
! There must be a single root element
XML documents are trees, and not forests
! All elements must be closed, unlike HTML, where many elements (example: <BR>) are not required to be closed
! Capitalization of opening and closing tags of elements must be consistent Many browsers allow inconsistent casing when using HTML elements (example: <table> </TABLE>), but inconsistent casing is not allowed in XML
! Elements must be nested correctly
! Attribute values must be enclosed in quotes Many browsers allow attribute values to be unquoted, but this is not allowed in XML
! 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
Trang 21XSD Fundamentals
! Simple and Complex Types
! Groups
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
One of the most important activities involved in designing and implementing Web Services is specifying datatypes that are passed to and returned by a Web Service Datatypes must be defined unambiguously in the specifications The XSD language is best suited for defining such document specifications Let us examine some of the key concepts of XSD
This topic is intended only to provide a brief introduction of some of the major concepts of XSD The complete specifications can be found at
designing and implementing
Web Services is specifying
datatypes that are passed to
and returned by a Web
Service
Delivery Tip
This topic is not intended to
be a complete tutorial on
XSD Avoid getting into any
more detail than what is
Trang 22Simple and Complex Types
An XML schema can consist of elements that are simple types (datatypes) or complex types (datatypes) A complex type can contain child elements as well
as attributes in its content A simple type can contain neither child elements nor attributes in its content
Consider the following XML code:
<xsd:element name="type" type="xsd:string"/>
<xsd:element name="balance" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="status" type="xsd:string"/>
</xsd:complexType>
In the preceding example, you can further constrain the element named type to
restrict it to a 2 character code that is made up of only upper-case letters You
can do this by defining a simple type and redefining the type element as
To explain the sample code
to the students, open the file
<install
folder>\Democode\Mod03\
XSD Simple and Complex
Types.txt
Trang 23Groups
When designing the structure of a document, it can be useful to define groups
of elements or attributes that can be used in the definition of many different complex types For example, you might want to define different types of accounts such as checking, savings, credit card, etc It would be inconvenient to repeatedly list the common elements in each account type in the type definition
In such situations, XSD groups are useful
In continuation with the preceding example, you can define a group of common elements for all types of accounts as follows:
<xsd:group name="acct">
<xsd:sequence>
<xsd:element name="description" type="xsd:string"/> <xsd:element name="number" type="xsd:string"/>
<xsd:element name="type" type="acctTypeCode"/>
<xsd:element name="balance" type="xsd:decimal"/>
To explain the sample code
to the students, open the file
<install
folder>\Democode\Mod03\
XSD Groups.txt
Trang 24***************************** ILLEGAL FOR NON - TRAINER USE ******************************
In addition to supporting simple and complex types and groups, XSD also supports two features: compositors and derivation
Compositors
A compositor is an element that allows you to associate groups of elements in various ways XSD provides the following compositors:
! xsd:sequence The xsd:sequence compositor is used to define an ordered sequence of child elements In the previous example, xsd:sequence defines an ordered
sequence of child elements in a complex type
! xsd:choice The xsd:choice compositor is used to define a list of choices, which can be
a set of elements, groups, or compositors You can use the xsd:choice
compositor in a schema to specify that the XML document validated by the schema can contain one of the choices from a given list
simple and complex types
and groups, XSD also
supports two features:
compositors and derivation
Delivery Tip
To explain the sample code
to the students, open the file
<install
folder>\Democode\Mod03\
XSD Compositors.txt
Trang 25For example, the xsd:choice compositor in the following schema code
specifies that the XML document validated by the schema can contain either
a fullname element or a sequence of elements that describe a customer name:
</xsd:choice>
</xsd:complexType>
! xsd:all The xsd:all compositor defines an unordered list of elements, groups, or compositors For example, the xsd:all compositor in the following schema
code specifies that the XML document validated by the schema can contain the checking, savings, and creditcard elements in any order:
<xsd:complexType name="listOfAccts">
<xsd:all minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="checking" minOccurs="0" />
<xsd:element ref="savings" minOccurs="0" />
<xsd:element ref="creditcard" minOccurs="0" />
</xsd:all>
</xsd:complexType>