Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives...

29
Lecture 12: Web Services

Transcript of Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives...

Page 1: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

Lecture 12:

Web Services

Page 2: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 3: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

12-3MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET

Part 1

• Background…

Page 4: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 5: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 6: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 7: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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…

Page 8: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 9: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

12-9MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET

Part 2

• Demos…

Page 10: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 11: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 12: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

12-12MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET

Part 3

• Consuming a web service…

Page 13: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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!

Page 14: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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…"

Page 15: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

12-15MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET

(2) Creating WinForm app

• Create client-side WinForm app as you normally would:

WebBrowser controlListBox

Page 16: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 17: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 18: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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 );}

Page 19: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

12-19MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET

(5) Run!

Page 20: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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 …

Page 21: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 22: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

12-22MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET

Why does Google do this? Amazon?

• Make $$– they charge commercial customers per search / sale…

Page 23: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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…

Page 24: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

12-24MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET

Part 4

• Creating a web service…

Page 25: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 26: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

12-26MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET

(1) Select template

• Start by creating a project of type “ASP.NET Web Service”

Page 27: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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; } . . .

}

Page 28: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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

Page 29: Lecture 12: Web Services. 12-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Web Services are objects callable across a network.

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