Lab #2: Web API Programming Exercises By J. H. Wang Dec. 19, 2011
Outline Yahoo! Term Extraction API Bing Search API Others
Platform and compiler (The same as networking programming in any language on any platform.)
Web API programming exercises 1.Network programming Sending a request packet Parsing the response from Web server 2.To start programming with Web API – Document: e.g. – Register an API key – Start using the API 3.Protocol and message format – Prepare parameters in request – Parsing responses
Network Programming Sending an HTTP packet Parsing the HTTP responses
Yahoo Term Extraction API Document: – Register an API key – Start using the API – Request method: HTTP POST – Response format: JSON, XML
Yahoo! 搜尋『斷章取義』 API Method] – API Method: ws: (word segmentation) ke: (keyword extraction) – appid= – content= (encoded in UTF-8) – format=json (or xml)
HTTP POST E.g.: &format=xml&content= POST /cas/v1/ke HTTP/1.1\r\n Host: asia.search.yahooapis.com\r\n Content-Length: \r\n Content-Type: application/x-www-form- urlencoded\r\n \r\n appid= &format=xml&content=
Yahoo Term Extraction Web Service mExtraction.html mExtraction.html Sample request: – ermExtraction – appid: YahooDemo – context: Italian sculptors and painters of the renaissance favored the Virgin Mary for inspiration. – query: madonna
Sample response – italian sculptors virgin mary painters renaissance inspiration
Parsing JSON Output Document: Implementations – JSON parser in C, C++, Java, Perl, PHP, Python, … – Ex. JSON_checker, and JSON_parser in C
Bing Search API Document: us/library/dd aspxhttp://msdn.microsoft.com/en- us/library/dd aspx – Search: Web, images, news, video, … – Register an API key – Start using the API – Request method: HTTP GET – Response format: JSON, XML, SOAP
Bing Web Search Example in Web search – Sample request: – – AppId= – Sources=Web – Query=msdn blogs
HTTP GET E.g.: &Sources=Web&Query= GET /json.aspx?AppId= &Sources=Web&Query= HTTP/1.1\r\n Connection: close\r\n Host: api.bing.net\r\n \r\n
Request Example your AppId here&Query=msdn blogs&Sources=Web&Version=2.0&Market=e n- us&Adult=Moderate&Options=EnableHighligh ting&Web.Count=10&Web.Offset=0&Web.Opt ions=DisableHostCollapsing+DisableQueryAlte rations&JsonType=callback&JsonCallback=Sea rchCompleted
Response Example { "SearchResponse":{ "Version":"2.0", "Query":{ "SearchTerms":"msdn blogs" }, "Web":{ "Total":1490, "Offset":0, "Results":[ { "Title":"Monads", "Description":"Monad 1 2 Manifesto. Jeffrey P. Snover. Aug 8, Version 1.2. What is Monad? Monad is the next generation platform for administrative automation.", "Url":" ", "DisplayUrl":" 7.ashx", "DateTime":" T05:27:14Z" }, …, { "Title":"Software Design Invitation Proposal Template for Imagine Cup", "Description":"SINGPAPORE SOFTWARE DESIGN INVITATIONAL SUBMISSION PROPOSAL TEMPLATE. NOTE: This section contains a template, not a fixed format. Please return soft copy only – hardcopies will not be...", "Url":" "DisplayUrl":" hx", "DateTime":" T18:13:07Z" } ] } } }
Thanks for Your Attention!