290 likes | 405 Views
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.
E N D
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
Part 1 • Background…
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 Web server obj obj Client Web Page obj Server
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 Web server HTML Web Page browser obj obj Web Service XML app obj Server
(1) http://.../WebForm1.aspx (3) view (2) HTML <html> <head> <title>WebForm1</title> </head> <body> <form name="Form1" ...> <scan ...> <input ...> . . . </form> </body> </html> (1) Web forms • An example of a traditional HTML-based web app: Web server Browser Web Page
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…
<Add> <x>20</x> <y>99</y> </Add> (1) XML (2) call (3) XML (4) view int Add(int x, int y) { return x + y; } <Add> <result>119</result> </Add> obj (2) Web services • Here's a GUI app built using a calculator web service… Web server GUI.exe Web Service
Part 2 • Demos…
Demo #1 • Amazon web service • Amazon.com makes product info available via a web service • 10% of their business is currently generated this way
Demo #2 • TerraServer web service • TerraServer contains global satellite images of Earth's surface • freely-available via this Microsoft-sponsored web service
Part 3 • Consuming a web service…
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: • 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!
(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…"
(2) Creating WinForm app • Create client-side WinForm app as you normally would: ListBox WebBrowser control
(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
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 Web server WSDL document Client Web Service
(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 ); }
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 …
obj 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 Web server WSDL client app obj obj method call method call stub proxy SOAP msg (XML) HTTP request
Why does Google do this? Amazon? • Make $$ • they charge commercial customers per search / sale…
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…
Part 4 • Creating a web service…
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
(1) Select template • Start by creating a project of type “ASP.NET Web Service”
(2) Code as usual • Web service is just a class with web-callable methods • denoted by WebMethodattribute • 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; } . . . }
To use this web service… • Give your clients these URLs: • WSDL: http://servername/WebService1/Service1.asmx?wsdl • Web reference: http://servername/WebService1/Service1.asmx
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