· Types
XMLRPC-soap RPC-restful RPC
This article will analyze three different RPC methods from three different perspectives: code, request, and response.
· XMLRPC/jsonrpc (Bollywood)
This is an example of Leonard Richard's book restful Web Services.
An example in Ruby:
#! /Usr/bin/ruby-W # Xmlrpc-upc.rb Require 'xmlrpc/client' Def find_product (UPC) Server = XMLRPC: client. new2 ('HTTP: // www.upcdatabase.com/rpc ') Begin Response = server. Call ('lookupuppc', UPC) Rescue XMLRPC: faultexception => E Puts "error :" Puts E. faultcode Puts E. faultstring End End Puts find_product ("001441000055") ['description'] # "Trader Joe's Thai rice noodles" |
Rewrite in Python:
#! /Usr/bin/Python Import xmlrpclib Def find_product (UPC ): Server = xmlrpclib. serverproxy ('HTTP: // www.upcdatabase.com/rpc ') Result = server. lookupupc (UPC) If type (result) = dict: If result ['found ']: Return result ['description'] Else: Return result ['message'] Return "" Print find_product ("001441000055 ") # Trader Joe's Thai rice noodles |
Request:
<? XML version = "1.0"?> <Methodcall> <Methodname> lookupupc </methodname> <Params> <Param> <value> <string> 001441000055 </string> </value> </param> </Params> </Methodcall> |
Response:
HTTP/1.1 200 OK Date: Mon, 16 Aug 2010 13:49:58 GMT Server: Apache Vary: Accept-Encoding Connection: Close Content-Type: text/XML <? XML version = "1.0"?> <Methodresponse> <Params> <Param> <value> <struct> <Member> <Name> UPC </Name> <value> <string> 001441000055 </string> </value> </Member> <Member> <Name> pendingupdates </Name> <value> <int> 0 </int> </value> </Member> <Member> <Name> iscoupon </Name> <value> <Boolean> 0 </Boolean> </value> </Member> <Member> <Name> EAN </Name> <value> <string> 0001441000055 </string> </value> </Member> <Member> <Name> issuercountrycode </Name> <value> <string> us </string> </value> </Member> <Member> <Name> found </Name> <value> <Boolean> 1 </Boolean> </value> </Member> <Member> <Name> description </Name> <value> <string> Trader Joe's Thai rice noodles </string> </value> </Member> <Member> <Name> size </Name> <value> <string> 12 oz. </string> </value> </Member> <Member> <Name> message </Name> <value> <string> database entry found </string> </value> </Member> <Member> <Name> issuercountry </Name> <value> <string> United States </string> </value> </Member> <Member> <Name> lastmodified </Name> <value> <string> 18:11:27 </string> </value> </Member> <Member> <Name> upce </Name> <value> <string> 00144155 </string> </value> </Member> </Struct> </value> </Param> </Params> </Methodresponse> |
· Soap RPC (Hollywood)
In actual, soap is not just about HTTP. It's also about SMTP and some other protocols.
The reason why it's based on HTTP is just for pass through firewall.
The following example calls the soap API of chinastock to obtain a stock market in Shanghai and Shenzhen. In fact, many fund websites and stock market websites do this.
// 61.147.124.120 // Class soapquery { Public static string getsource () { Return "http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx "; } // Getstockinfo // Public static string getstockinfo (string code) { Chinastockwebservice.cn.com. webxml. www. chinastockwebservice oservice = new chinastockwebservice.cn.com. webxml. www. chinastockwebservice (); String [] info_byte = oservice. getstockinfobycode (CODE ); String strinfo = ""; Foreach (string strline in info_byte) { Strinfo = strinfo + "\ n" + strline; } Return strinfo; } // Save stock image to local file // Public static void getstockimage (string code, string filename) { Chinastockwebservice.cn.com. webxml. www. chinastockwebservice oservice = New chinastockwebservice.cn.com. webxml. www. chinastockwebservice (); Byte [] image_bytes = oservice. getstockimagebytebycode (CODE ); Image image = covertutil. bytearraytoimage (image_bytes ); Image. Save (filename ); } }; |
Running result
Request
// Request from commview HTTP Analyzer Post/WebServices/chinastockwebservice. asmx HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client protocol 2.0.50727.3603) Vsdebuggercausalitydata: uidpowoeuujzbqrjtxafca/wshkaaaaa1gzbkv7onegsz5se4/oabj00bq ++ h01lmgvgjkwdaa4acqaa Content-Type: text/XML; charset = UTF-8 Soapaction: "http://WebXml.com.cn/getStockInfoByCode" HOST: www.webxml.com.cn Content-Length: 354 Secondary CT: 100-continue Connection: keep-alive HTTP/1.1 100 continue XML version = "1.0" encoding = "UTF-8"?> <Soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema"> <soap: body> <getstockinfobycode xmlns = "http://WebXml.com.cn/"> <thestockcode> sh120001 </thestockcode> </getstockinfobycode> </soap: Body> </soap: envelope> |
Response
// Response from commview HTTP Analyzer HTTP/1.1 200 OK Date: Mon, 16 Aug 2010 07:44:34 GMT Server: Microsoft-Microsoft IIS/6.0 X-powered-by: ASP. NET X-ASPnet-version: 2.0.50727 Cache-control: private, Max-age = 0 Content-Type: text/XML; charset = UTF-8 Content-Length: 832 <? XML version = "1.0" encoding = "UTF-8"?> <Soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema"> <soap: body> <getstockinfobycoderesponse xmlns = "http://WebXml.com.cn/"> <getstockinfobycoderesult> <string> shstock01 </string> <string> ä~~~~~~~~• ° </string> <string> 15:03:09 </string> <string> 2661.708 </string> <string> 2606.700 </string> <string> 2603.792 </string> <string> 55.01 </string>> <string> 2596.771 </string> <string> 2665.314 </string> <string> 2.11% </string> <string> 1258628.23 </string> <string> 13073685.1353 </string> <string/> <string/> </getstockinfobycoderesult> </getstockinfobycoderesponse> </soap: body> </soap: envelope> |
· Restful RPC (Original Ecology)
{Code} // 60.28.0.266 // Class restfulquery { Public static string getsource () { Return "http://hq.sinajs.cn "; } Public static string getstockinfo (string code) { String uri = "http://hq.sinajs.cn/list=" + code; Httpwebrequest request = createwebrequest (URI ); // Request. transferencoding = "GBK "; Httpwebresponse response = (httpwebresponse) request. getresponse (); Streamreader responsestream = new streamreader (response. getresponsestream (), encoding. getencoding ("GBK ")); Return responsestream. readtoend (); } // Save stock image to local file Public static void getstockimage (string code, string filename) { String uri = "http://image.sinajs.cn/newchart/daily/n/" + code + ". GIF "; Try { Httpwebrequest request = createwebrequest (URI ); Httpwebresponse response = (httpwebresponse) request. getresponse (); Image image = image. fromstream (response. getresponsestream ()); Image. Save (filename ); } Catch (exception E) { MessageBox. Show ("failed to query stock infromation "); Filename = ""; } } Public static httpwebrequest createwebrequest (string URI) { Return (httpwebrequest) webrequest. Create (URI ); } }; |
Running result
Request
// Request from commview HTTP Analyzer GET/list = sh000001 HTTP/1.1 HOST: hq.sinajs.cn |
Response
// Response from commview HTTP Analyzer HTTP/1.1 200 OK Cache-control: No-Cache Content-Length: 164 Connection: keep-alive Content-Type: Application/X-JavaScript; charset = GBK VaR hq_str_sh1_01 = "é ö ö¤ ö¸ ý, 2603.792, 2606.700, 2661.708, 2665.314, 2596.771, 125862823,130742551353, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2010-08-16, 15: 03: 09 "; |
The Restful architecture means that the method information is reflected in the HTTP method;
XMLRPC and soap, all requests are based on post method.
An interesting example of AWS is that Amazon called non-soap API as query API, not restful API.
ROA is mentioned in Leonard's book; it indicates that the scope information exists in Uri;
For a roa-based restful web service, we should be able to get a basic understanding of client requests from the URI, while the HTTP request section defines the specific information of the method.
Possible representation format: XML, JSON, Atom publish, XHTML ..