Lecture 12:
Web Services
Objectives
“Web Services are objects callable across a network. The magic is
that web services are platform-independent, for the first time
allowing easy creation of heterogeneous systems...”
•
•
•
•
Background
Some demos
Consuming a web service
Creating a web service
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-2
Part 1
• Background…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-3
Web-based applications
• Web server should be viewed as just another application tier
• Motivation:
– web-based app is now accessible across the internet
– web-based app is now accessible from *any* client platform
Client
Web
Page
Web server
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
obj
obj
obj
Server
12-4
Types of web applications
• Two types:
– Web forms:
– Web services:
browser
app
HTML
XML
web app returns HTML — i.e. data + presentation
web app returns XML — just the raw data
Web
Page
Web
Service
Web server
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
obj
obj
obj
Server
12-5
(1) Web forms
• An example of a traditional HTML-based web app:
(1) http://.../WebForm1.aspx
Web
Page
Browser
(2) HTML
(3) view
Web server
<html>
<head>
<title>WebForm1</title>
</head>
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
<body>
<form name="Form1" ...>
<scan ...>
<input ...>
.
.
.
</form>
</body>
</html>
12-6
Problems with form-based web apps
• Data intermixed with HTML
– what if I just want the data?
• Based on user – computer interaction
– what if I want to connect computers?
• Web services created to solve these problems…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-7
(2) Web services
• Here's a GUI app built using a calculator web service…
<Add>
<x>20</x>
<y>99</y>
</Add>
(1) XML
Web
Service
GUI.exe
(3) XML
(4) view
(2) call
obj
Web server
<Add>
<result>119</result>
</Add>
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
int Add(int x, int y)
{ return x + y; }
12-8
Part 2
• Demos…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-9
Demo #1
• Amazon web service
– Amazon.com makes product info available via a web service
– 10% of their business is currently generated this way
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-10
Demo #2
• TerraServer web service
– TerraServer contains global satellite images of Earth's surface
– freely-available via this Microsoft-sponsored web service
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-11
Part 3
• Consuming a web service…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-12
Example
• Let's create a client that
consumes a web service…
• Google WebService App:
– GUI app that performs internet search using Google!
– keep in mind that Google is a web-farm of Linux machines
• 5-step process:
1.
2.
3.
4.
5.
sign-up for a free Google account
create WinForm app as usual
set reference to Google web service
call Google service like any other object
run!
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-13
(1) Getting a Google account
• It's free!
• Surf to http://www.google.com/apis/:
– follow step 2 to create a Google account (painless)
– reply to verification email
– you'll receive login key, e.g. "4a8/TvZQFH…"
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-14
(2) Creating WinForm app
• Create client-side WinForm app as you normally would:
ListBox
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
WebBrowser control
12-15
(3) Referencing Google web service
• Recall that you must reference a component before using it
• In the case of web services, set a Web Reference…
– for Google, reference http://api.google.com/GoogleSearch.wsdl
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-16
What did setting a reference really do?
• Setting a web reference requests WSDL doc from service
– WSDL = Web Service Description Language
– formal description of interface between client & service
WSDL document
Client
Web
Service
Web server
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-17
(4) Calling Google service
• Now create Google search object & call!
public void cmdSearch_Click(...)
{
GoogleSearchService google;
GoogleSearchResult
results;
// ask google to search for us...
google = new GoogleSearchService();
results = google.doGoogleSearch("4a8/TvZQFHID0WIWnL1CMmMx0sNqhG8H",
txtSearch.get_Text(), 0, 10, false, "", false, "", "", "");
// display resulting URLs...
for (int i=0; i<results.resultElements.length; i++)
lstURLs.get_Items().Add( results.resultElements[i].URL );
}
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-18
(5) Run!
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-19
What we just did…
• Connected two computers across the internet
– we're running Windows
– Google is running Linux
• And we did it via standard OOP
– no network programming, no TCP/IP, no XML, no …
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-20
How does it really work?
• Based on RPC (Remote Procedure Call) architecture:
– client calls proxy, which builds msg & sends to server
– corresponding stub on server receives msg & calls object
web service
WSDL
client app
obj
obj
method call
method call
obj
stub
proxy
Web server
SOAP msg (XML)
HTTP request
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-21
Why does Google do this? Amazon?
• Make $$
– they charge commercial customers per search / sale…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-22
Why are web services important?
• Work on any platform:
– Mac OS X, Windows, Linux, ...
• Work with most programming languages:
– J#, Java, C, C++, VB, C#, …
• Work with old, legacy hardware:
– new systems can interact with old…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-23
Part 4
• Creating a web service…
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-24
Creating a web service
• Trivial to do if you're using Visual Studio .NET…
– note that IIS must be installed for this to work with VS .NET
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-25
(1) Select template
• Start by creating a project of type “ASP.NET Web Service”
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-26
(2) Code as usual
• Web service is just a class with web-callable methods
– denoted by WebMethod attribute
– code, build, & that's it!
public class Service1 extends System.Web.Services.WebService
{
/** @attribute WebMethod() */
public int Add(int x, int y)
{
return x + y;
}
.
.
.
}
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-27
To use this web service…
• Give your clients these URLs:
– WSDL:
http://servername/WebService1/Service1.asmx?wsdl
– Web reference: http://servername/WebService1/Service1.asmx
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-28
Summary
• Pretty powerful stuff!
– OO construction of heterogeneous systems
• Based on lots of technology:
– XML for data format
– SOAP as communication protocol
– WSDL for formal description of web service
– ASP.NET, the web component of .NET
– proxy-stub distributed design
Introducing
Microsoft
J#
in Visual Studio
CS using
.NET
.NET
12-29
Descargar

13. Web Services - University of Illinois at Chicago