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
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.
Module 3: The Underlying Technologies of Web Services iii
Instructor 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
iv Module 3: The Underlying Technologies of Web Services
Module 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.
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
®
.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.
Topic Objective
To provide an overview of
the module topics and
objectives.
Lead-in
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
-
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.
Topic Objective
To introduce the topics in
this section.
Lead-in
In this section, you will learn
the fundamentals of HTTP
and how to make HTTP
requests using the .NET
Framework.
Module 3: The Underlying Technologies of Web Services 3
Overview of HTTP
!
Syntax of a URL
!
Example
!
Stateless Protocol
http://host[:port][path[?querystring]]
http://host[:port][path[?querystring]]
/> />
*****************************
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:
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.
Topic Objective
To explain URLs and how
HTTP is a stateless
protocol.
Lead-in
Resource locations are
specified in HTTP through
URLs.
Note
Example
4 Module 3: The Underlying Technologies of Web Services
Structures 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
<?xml version="1.0" encoding="utf-8"?>
<Symbol=MSFT />
Topic Objective
To describe the structures of
an HTTP request and
response.
Lead-in
HTTP requests and
responses have simple
structures.
Syntax
Example
Module 3: The Underlying Technologies of Web Services 5
The first line in an HTTP request is known as the request line, and the methods
that are supported by a request are as follows:
!
OPTIONS
!
GET
!
HEAD
!
POST
!
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
yntax
6 Module 3: The Underlying Technologies of Web Services
The GET and POST Methods
!
HTTP-GET
!
HTTP-POST
GET /Trading/GetStockPrice.asp?Symbol=MSFT HTTP/1.1
Host: localhost
GET /Trading/GetStockPrice.asp?Symbol=MSFT HTTP/1.1
Host: localhost
POST /Trading/GetStockPrice.asp HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 11
Symbol=MSFT
POST /Trading/GetStockPrice.asp HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-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.
Module 3: The Underlying Technologies of Web Services 7
HTTP-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.
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
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
HTTP-GET and HTTP-
POST requests.
Lead-in
Issuing an HTTP request
and receiving a response is
easy using the .NET
Framework.
Note
Module 3: The Underlying Technologies of Web Services 9
StreamReader 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.
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);
9. sw.Close();
10.
11. HttpWebResponse res = (HttpWebResponse)!
req.GetResponse();
12. s = res.GetResponseStream();
Topic Objective
To walk through the code for
a sample synchronous
HTTP request.
Lead-in
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.
Module 3: The Underlying Technologies of Web Services 11
13. StreamReader sr = new StreamReader(s,Encoding.ASCII);
14. StringBuilder sb = new StringBuilder();
15. char [] data = new char[1024];
16. int nBytes;
17. do
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
Code 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
an asynchronous request:
1. Stream s;
2. HttpWebRequest req = (HttpWebRequest )!
WebRequest.Create(url);
3. req.ContentType=contentType;
4. req.Method = method;
5. req.ContentLength=content.Length;
6. s = req.GetRequestStream();
7. StreamWriter sw = new StreamWriter(s);
8. sw.Write(content);
9. sw.Close();
10.
11. Handler h = new Handler();
12. AsyncCallback callback = new !
AsyncCallback(h.Callback)
13. // Pass the request object as the state object
14. req.BeginGetResponse(callback, req);
15.
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.
25. HttpWebResponse res = (HttpWebResponse)!
req.EndGetResponse(ar);
26. // Start reading data from the response stream.
27. Stream s = res.GetResponseStream();
28.
29. StreamReader sr = new StreamReader(s,Encoding.ASCII);
30. StringBuilder sb = new StringBuilder();
31. char [] data = new char[1024];
32. int nBytes;
33. do
34. {
35. nBytes = sr.Read(data,0,(int)1024);
36. sb.Append(data);
37. } while (nBytes == 1024);
38. ...
39. // continue processing
40. }
41. }
The functionality implemented by the preceding code can be described as
follows:
!
In lines 1 through 9, an HTTP request is setup exactly the way it is done in a
synchronous operation.
!
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.
14 Module 3: The Underlying Technologies of Web Services
"
""
"
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.
Module 3: The Underlying Technologies of Web Services 15
Overview 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.
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
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.
16 Module 3: The Underlying Technologies of Web Services
Well-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.