XML-RPC

XML-RPC

XML-RPC是一個遠程過程調用遠端程式呼叫)(remote procedure call,RPC)的分散式計算協定,通過XML將調用函式封裝,並使用HTTP協定作為傳送機制。

基本介紹

關於XML-RPC,基本介紹,Request example,Response example,XML-RPC入門程式,基本做法,管理器類,伺服器類,客戶程式,RPC和RMI的簡單比較,調用形式,classname.methodname的形式,methodname,匹配成功後,

關於XML-RPC

基本介紹

XML-RPC是工作在Internet上的遠程過程調用協定。一個XML-RPC訊息就是一個請求體為xml的http-post請求,被調用的方法在伺服器端執行並將執行結果以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
<?xmlversion="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
<?xmlversion="1.0"?><methodResponse><params><param><value><string>SouthDakota</string></value></param></params></methodResponse>

XML-RPC入門程式

基本做法

以下的入門程式包括一個管理器(HelloHandler)、一個伺服器(HelloServer)、一個客戶程式(HelloClient)。
首先要做的是創建用於遠程過程調用的類和方法,人們常常稱之為管理器。Xml-rpc管理器是一個方法和方法集,它接受xml-rpc請求,並對請求的內容進行解碼,再向一個類和方法發出請求。

管理器類

packagexmlRpc;/***@authortrier**<b><code>HelloHandler</code></b>isasimplehandlerthancan*beregisteredwithanXML-RPCserver*/publicclassHelloHandler{publicStringsayHello(Stringname){return"Hello"+name;}}
伺服器程式將創建的管理器註冊到伺服器上,並為伺服器指明應用程式其他特定的參數。

伺服器類

packagexmlRpc;/****<b><code>HelloServer</code></b>isasimpleXML-RPCserver*thatwilltakethe<code>HelloHandler</code>classavailable*forXML-PRCcalls.*<o:p*/importorg.apache.xmlrpc.WebServer;importorg.apache.xmlrpc.XmlRpc;importjava.IOException;publicclassHelloServer{publicstaticvoidmain(String[]args){if(args.length<1){System.out.println("Usage:javaHelloServer[port]");System.exit(-1);}try{XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");//starttheserverSystem.out.println("StartingXML-RPCServer......");WebServerserver=newWebServer(Integer.parseInt(args[0]));//registerourhandlerclassserver.addHandler("hello",newHelloHandler());System.out.println("Nowacceptingrequests......");}catch(ClassNotFoundExceptione){System.out.println("CouldnotlocateSAXDriver");}catch(IOExceptione){System.out.println("Couldnotstartserver:"+e.getMessage());}}}

客戶程式

packagexmlRpc;/****<b><code>HelloClient</code></b>isasimpleXML-RPCclient*thatmakesanXML-RPCrequestto<code>HelloServer</code>*/importjava.i.IOException;importjava.util.Vector;importorg.apache.xmlrpc.XmlRpc;importorg.apache.xmlrpc.XmlRpcClient;importjava.t.MalformedURLException;importorg.apache.xmlrpc.XmlRpcException;publicclassHelloClient{publicstaticvoidmain(String[]args){if(args.length<1){System.out.println("Usage:javaHelloClient[yourname]");System.exit(-1);}try{//UsetheApacheXerecesSAXDriverXmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");//SpecifytheserverXmlRpcClientclient=newXmlRpcClient("http://localhost:8585");//createrequestVectorparams=newVector();params.addElement(args[0]);//makearequestandprinttheresultStringresult=(String)client.execute("hello.sayHello",params);System.out.println("Responsefromserver:"+result);}catch(ClassNotFoundExceptione){System.out.println("CouldnotlocateSAXDriver");}catch(MalformedURLExceptione){System.out.println("IncorrectURLfroxml-rpcserverforamt:"+e.getMessage());}catch(XmlRpcExceptione){System.out.println("XmlRpcException:"+e.getMessage());}catch(IOExceptione){System.out.println("IOException:"+e.getMessage());}}}

RPC和RMI的簡單比較

調用形式

在RMI和RPC之間最主要的區別在於方法是如何被調用的。在RMI中,遠程接口使每個遠程方法都具有方法簽名。如果一個方法在伺服器上執行,但是沒有相匹配的簽名被添加到這個遠程接口上,那么這個新方法就不能被RMI客戶方所調用。

classname.methodname的形式

在RPC中,當一個請求到達RPC伺服器時,這個請求就包含了一個參數集和一個文本值,通常形成“classname.methodname”的形式。

methodname

這就向RPC伺服器表明,被請求的方法在為“classname”的類中,名叫“methodname”。然後RPC伺服器就去搜尋與之相匹配的類和方法,並把它作為那種方法參數類型的輸入。這裡的參數類型是與RPC請求中的類型是匹配的。

匹配成功後

一旦匹配成功,這個方法就被調用了,其結果被編碼後返回客戶方。

相關詞條

熱門詞條

聯絡我們