Post on 02-Apr-2015
Lecture 12:
Web Services
12-2MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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
12-3MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
Part 1
• Background…
12-4MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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
obj obj
objWeb server
Client WebPage
Server
12-5MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
Types of web applications
• Two types:– Web forms: web app returns HTML — i.e. data +
presentation– Web services: web app returns XML — just the raw data
obj obj
obj
browser
Web server
HTMLWebPage
app
Server
WebService
XML
12-6MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(1) Web forms
• An example of a traditional HTML-based web app:
Browser
Web server
WebPage
(1) http://.../WebForm1.aspx
(2) HTML
<html> <head> <title>WebForm1</title> </head>
<body> <form name="Form1" ...> <scan ...> <input ...> . . . </form> </body></html>
(3) view
12-7MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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…
12-8MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(2) Web services
• Here's a GUI app built using a calculator web service…
GUI.exe
Web server
WebService
(3) XML
<Add> <result>119</result></Add>
(1) XML
<Add> <x>20</x> <y>99</y></Add>
obj
int Add(int x, int y){ return x + y; }
(2) call
(4) view
12-9MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
Part 2
• Demos…
12-10MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
Demo #1
• Amazon web service– Amazon.com makes product info available via a web service– 10% of their business is currently generated this way
12-11MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
Demo #2
• TerraServer web service– TerraServer contains global satellite images of Earth's surface– freely-available via this Microsoft-sponsored web service
12-12MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
Part 3
• Consuming a web service…
12-13MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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. sign-up for a free Google account
2. create WinForm app as usual
3. set reference to Google web service
4. call Google service like any other object
5. run!
12-14MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(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…"
12-15MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(2) Creating WinForm app
• Create client-side WinForm app as you normally would:
WebBrowser controlListBox
12-16MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(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
12-17MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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
Client
Web server
WebService
WSDL document
12-18MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(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 );}
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 );}
12-19MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(5) Run!
12-20MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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 …
12-21MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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
obj
obj
Web server
objclient app
proxy
method call
HTTP request
SOAP msg (XML)
stub
web service
method call
WSDL
12-22MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
Why does Google do this? Amazon?
• Make $$– they charge commercial customers per search / sale…
12-23MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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…
12-24MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
Part 4
• Creating a web service…
12-25MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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
12-26MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(1) Select template
• Start by creating a project of type “ASP.NET Web Service”
12-27MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
(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; } . . .
}
public class Service1 extends System.Web.Services.WebService{ /** @attribute WebMethod() */ public int Add(int x, int y) { return x + y; } . . .
}
12-28MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
To use this web service…
• Give your clients these URLs:
– WSDL: http://servername/WebService1/Service1.asmx?wsdl
– Web reference:http://servername/WebService1/Service1.asmx
12-29MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET
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