Webservice - XML-RPC
http://baike.baidu.com/view/643379.htmXML-RPC的全稱(chēng)是XML Remote Procedure Call,即XML遠(yuǎn)程方法調(diào)用。 它是一套允許運(yùn)行在不同操作系統(tǒng)、不同環(huán)境的程序?qū)崿F(xiàn)基于Internet過(guò)程調(diào)用的規(guī)范和一系列的實(shí)現(xiàn)。 這種遠(yuǎn)程過(guò)程調(diào)用使用http作為傳輸協(xié)議,XML作為傳送信息的編碼格式。Xml-Rpc的定義盡可能的保持了簡(jiǎn)單,但同時(shí)能夠傳送、處理、返回復(fù)雜的數(shù)據(jù)結(jié)構(gòu)。 XML-RPC是工作在Internet上的遠(yuǎn)程過(guò)程調(diào)用協(xié)議。一個(gè)XML-RPC消息就是一個(gè)請(qǐng)求體為xml的http-post請(qǐng)求,被調(diào)用的方法在服務(wù)器端執(zhí)行并將執(zhí)行結(jié)果以xml格式編碼后返回。 Request example Here's an example of an XML-RPC request: POST /RPC2 HTTP/1.0User-Agent: Frontier/5.1.2 (WinNT)Host: betty.userland.comContent-Type: text/xmlContent-length: 181 <?xml version="1.0"?> <methodCall> <methodName>examples.getStateName</methodName> <params> <param> <value><i4>41</i4></value> </param> </params> </methodCall> Response example Here's an example of a response to an XML-RPC request: HTTP/1.1 200 OKConnection: closeContent-Length: 158Content-Type: text/xmlDate: Fri, 17 Jul 1998 19:55:08 GMTServer: UserLand Frontier/5.1.2-WinNT <?xml version="1.0"?> <methodResponse> <params> <param> <value><string>South Dakota</string></value> </param> </params> </methodResponse> 二 XML-RPC入門(mén)程序 以下的入門(mén)程序包括一個(gè)管理器(HelloHandler)、一個(gè)服務(wù)器(HelloServer)、一個(gè)客戶程序(HelloClient)。 首先要做的是創(chuàng)建用于遠(yuǎn)程過(guò)程調(diào)用的類(lèi)和方法,人們常常稱(chēng)之為管理器。Xml-rpc管理器是一個(gè)方法和方法集,它接受xml-rpc請(qǐng)求,并對(duì)請(qǐng)求的內(nèi)容進(jìn)行解碼,再向一個(gè)類(lèi)和方法發(fā)出請(qǐng)求。 //管理器類(lèi) package xmlRpc; /** * @author trier * * <b><code>HelloHandler</code></b> is a simple handler than can * be registered with an XML-RPC server */ public class HelloHandler { public String sayHello(String name){ return "Hello " + name; } } 服務(wù)器程序?qū)?chuàng)建的管理器注冊(cè)到服務(wù)器上,并為服務(wù)器指明應(yīng)用程序其他特定的參數(shù)。 //服務(wù)器類(lèi) package xmlRpc; /** * * <b><code>HelloServer</code></b> is a simple XML-RPC server * that will take the <code>HelloHandler</code> class available * for XML-PRC calls. * <o:p */ import org.apache.xmlrpc.WebServer; import org.apache.xmlrpc.XmlRpc; import java.io.IOException; public class HelloServer { public static void main(String[] args){ if(args.length<1){ System.out.println("Usage: java HelloServer [port]"); System.exit(-1); } try{ XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser"); //start the server System.out.println("Starting XML-RPC Server......"); WebServer server = new WebServer(Integer.parseInt(args[0])); //register our handler class server.addHandler("hello",new HelloHandler()); System.out.println("Now accepting requests......"); }catch(ClassNotFoundException e){ System.out.println("Could not locate SAX Driver"); }catch(IOException e){ System.out.println("Could not start server: "+e.getMessage()); } } } //客戶程序 package xmlRpc; /** * * <b><code>HelloClient</code></b> is a simple XML-RPC client * that makes an XML-RPC request to <code>HelloServer</code> */ import java.io.IOException; import java.util.Vector; import org.apache.xmlrpc.XmlRpc; import org.apache.xmlrpc.XmlRpcClient; import java.net.MalformedURLException; import org.apache.xmlrpc.XmlRpcException; public class HelloClient { public static void main(String[] args){ if(args.length<1){ System.out.println("Usage: java HelloClient [your name]"); System.exit(-1); } try{ //Use the Apache Xereces SAX Driver XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser"); //Specify the server XmlRpcClient client = new XmlRpcClient("http://localhost:8585"); //create request Vector params = new Vector(); params.addElement(args[0]); //make a request and print the result String result = (String)client.execute("hello.sayHello",params); System.out.println("Response from server: "+ result); }catch(ClassNotFoundException e){ System.out.println("Could not locate SAX Driver"); }catch(MalformedURLException e){ System.out.println("Incorrect URL fro xml-rpc server foramt:"+e.getMessage()); }catch(XmlRpcException e){ System.out.println("XmlRpcException :"+e.getMessage()); }catch(IOException e){ System.out.println("IOException:"+e.getMessage()); } } } 三 RPC和RMI的簡(jiǎn)單比較 在RMI和RPC之間最主要的區(qū)別在于方法是如何別調(diào)用的。在RMI中,遠(yuǎn)程接口使每個(gè)遠(yuǎn)程方法都具有方法簽名。如果一個(gè)方法在服務(wù)器上執(zhí)行,但是沒(méi)有相匹配的簽名被添加到這個(gè)遠(yuǎn)程接口上,那么這個(gè)新方法就不能被RMI客戶方所調(diào)用。在RPC中,當(dāng)一個(gè)請(qǐng)求到達(dá)RPC服務(wù)器時(shí),這個(gè)請(qǐng)求就包含了一個(gè)參數(shù)集和一個(gè)文本值,通常形成“classname.methodname”的形式。這就向RPC服務(wù)器表明,被請(qǐng)求的方法在為“classname”的類(lèi)中,名叫“methodname”。然后RPC服務(wù)器就去搜索與之相匹配的類(lèi)和方法,并把它作為那種方法參數(shù)類(lèi)型的輸入。這里的參數(shù)類(lèi)型是與RPC請(qǐng)求中的類(lèi)型是匹配的。一旦匹配成功,這個(gè)方法就被調(diào)用了,其結(jié)果被編碼后返回客戶方。
posted on 2011-01-05 11:23 w@ns0ng 閱讀(518) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): J2EE 、web service