150 likes | 251 Vues
Introduction to Web Services. Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th http://www.cpe.ku.ac.th/~cpj Computer Engineering Department Kasetsart University, Bangkok, Thailand. Traditional World-Wide-Web. Designed for human-to-app interactions Information sharing
E N D
Introduction to Web Services Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th http://www.cpe.ku.ac.th/~cpj Computer Engineering Department Kasetsart University, Bangkok, Thailand
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
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
Web Services vs. Web Apps • Web application • Provides service directly to user • Web service or (Web API) • Provides service to other programs browser web server HTTP web server(service provider) app(service user) HTTP
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
Sample SOAP Message • E.g., message requesting IBM stock price POST /InStock HTTP/1.1 Host: www.example.org Content-Type: application/soap+xml; charset=utf-8 Content-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> HTTP Header SOAP Envelope Desired operation(Verb) Object of interest(Noun)
Sample REST Request • E.g., message requesting IBM stock price The above corresponds to the URL GET /stock/ibm HTTP/1.1 Host: www.example.org HTTP Header Desired operation(Verb) Object of interest(Noun) http://www.example.org/stock/ibm
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>
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
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
Coding Example • E.g., accessing HostIP.info from Python import sys import json from urllib import urlencode from 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']
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