15
Introduction to Web Services Asst. Prof. Chaiporn Jaikaeo, Ph.D. [email protected] http://www.cpe.ku.ac.th/~cpj Computer Engineering Department Kasetsart University, Bangkok, Thailand

Introduction to Web Services Asst. Prof. Chaiporn Jaikaeo, Ph.D. [email protected] cpj Computer Engineering Department Kasetsart

Embed Size (px)

Citation preview

Introduction to Web Services

Asst. Prof. Chaiporn Jaikaeo, [email protected]

http://www.cpe.ku.ac.th/~cpjComputer Engineering Department

Kasetsart University, Bangkok, Thailand

2

Traditional World-Wide-Web

Designed for human-to-app interactions Information sharing (Non-automated) E-commerce

Built on top of HTTP – for data transfer HTML – for representing document

3

What's Next? Try taking humans out of the loop Systematic application-to-application

interaction over the web Automated e-commerce Resource sharing Distributed computing

Web services (another) effort to build distributed

computing platform for the web

4

Web application Provides service directly to user

Web service or (Web API) Provides service to other programs

Web Services vs. Web Apps

HTTP

HTTP

browser

app(service user)

web server

web server(service provider)

5

Web Service Protocols Simple Object Access Protocol (SOAP)

Service requests and responses are always done via XML messages, called SOAP envelope

Formally supported by World Wide Web Consortium (W3C)

Representational State Transfer (REST) Service requests are done via generic HTTP

commands: GET, POST, PUT, DELETE Simpler set of operations

Other Web Service Protocols

Web feeds RSS Atom

Remote procedure call JSON-RPC XML-RPC

JavaScript

6

7

Sample SOAP Message E.g., message requesting IBM stock

pricePOST /InStock HTTP/1.1Host: www.example.orgContent-Type: application/soap+xml; charset=utf-8Content-Length: 299 <?xml version="1.0"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> <soap:Header> </soap:Header> <soap:Body> <m:GetStockPrice xmlns:m="http://www.example.org/stock"> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body></soap:Envelope> SOAP Envelope

HTTP Header

Desired operation(Verb)

Object of interest(Noun)

8

Sample REST Request E.g., message requesting IBM stock

price

The above corresponds to the URL

GET /stock/ibm HTTP/1.1Host: www.example.org HTTP Header

http://www.example.org/stock/ibm

Desired operation(Verb)

Object of interest(Noun)

9

Data Interchange Formats XML – eXtensible Markup Language

E.g., RSS, Atom

Large collection of libraries for processing XML

<?xml version="1.0" encoding="ISO-8859-1"?>

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

xmlns="http://purl.org/rss/1.0/">

<title>Slashdot</title>

<link>http://slashdot.org/</link>

<description>News for nerds, stuff that matters</description>

<dc:subject>Technology</dc:subject>

<items>

<rdf:Seq>

<rdf:li rdf:resource="http://it.slashdot.org/story/11/02/14/0337211/Recent-HP-Laptops-

Shipped-CPU-Choking-Wi-Fi-Driver?from=rss" />

</ref:Seq>

</items>

</rdf:RDF>

10

Data Interchange Formats JSON – JavaScript Object Notation

Can be directly evaluated in JavaScript Typically smaller than XML Libraries for other languages are also available

{ "Person": { "firstName": "John", "lastName": "Smith", "age": 25, "Address": { "streetAddress":"21 2nd Street", "city":"New York", "state":"NY", "postalCode":"10021" }, "PhoneNumbers": { "home":"212 555-1234", "fax":"646 555-4567" } }}

<Person> <firstName>John</firstName> <lastName>Smith</lastName> <age>25</age> <Address> <streetAddress>21 2nd Street</streetAddress> <city>New York</city> <state>NY</state> <postalCode>10021</postalCode> </Address> <PhoneNumbers> <home>212 555-1234</home> <fax>646 555-4567</fax> </PhoneNumbers></Person> XML

JSON

11

Example –Wikipedia RESTful API reference

http://en.wikipedia.org/w/api.php

Example http://en.wikipedia.org/w/api.php?

format=json&action=query&titles=Kasetsart%20University&prop=revisions&rvprop=content

Example – HostIP.info RESTful Determines geographical location of

the specified IP address API documentation

http://www.hostip.info/use.html Example

http://api.hostip.info/get_json.php?ip=158.108.2.71

12

Coding Example E.g., accessing HostIP.info from Python

13

import sysimport jsonfrom urllib import urlencodefrom urllib2 import urlopen

response = urlopen('http://api.hostip.info/get_json.php?' + urlencode({ 'ip' : sys.argv[1], }))

data = json.loads(response.read())print 'IP Address: %s' % data['ip']print 'City: %s' % data['city']print 'Country: %s' % data['country_name']

14

Mashups A mashup is a webpage or application that

offers new services by combining data or functionality from two or more sources

Main characteristics of mashups Combination Visualization Aggregation

They aim to make existing data more useful Example: Wikipediavision

Google Maps + Wikipedia http://www.lkozma.net/wpv/index.html

More Resources Search engine for web APIs and mashups

http://www.programmableweb.com

15