XML-RPC入門

一、什麼是XML-RPC


xml-rpc 是一套允許運行在不同操作系統、不同環境的程序實現基於internet過程調用的規範和一系列的實現。
這種遠程過程調用使用http作爲傳輸協議,xml作爲傳送信息的編碼格式。Xml-Rpc的定義儘可能的保持了簡單,但同時能夠傳送、處理、返回複雜的數據結構。
Xml-rpc是工作在internet上的遠程過程調用協議。一個xml-rpc消息就是一個請求體爲xml的http-post請求,被調用的方法在服務器端執行並將執行結果以xml格式編碼後返回。
  1. Request example 
  2. Here's an example of an XML-RPC request: 
  3. POST /RPC2 HTTP1.0
  4. User-Agent: Frontier5.1.2 (WinNT)
  5. Host: betty.userland.com
  6. Content-Type: textxml
  7. Content-length: 181
  8.  
  9.  
  10. <?xml version="1.0"?>
  11. <methodCall>
  12.    <methodName>examples.getStateName</methodName>
  13.    <params>
  14.       <param>
  15.          <value><i4>41</i4></value>
  16.          </param>
  17.       </params>
  18.    </methodCall>
  1. Response example 
  2. Here's an example of a response to an XML-RPC request: 
  3. HTTP1.1 200 OK
  4. Connection: close
  5. Content-Length: 158
  6. Content-Type: textxml
  7. Date: Fri, 17 Jul 1998 19:55:08 GMT
  8. Server: UserLand Frontier5.1.2-WinNT
  9.  
  10.  
  11. <?xml version="1.0"?>
  12. <methodResponse>
  13.    <params>
  14.       <param>
  15.          <value><string>South Dakota</string></value>
  16.          </param>
  17.       </params>
  18.    </methodResponse>

二、xml-rpc入門程序


以下的入門程序包括一個管理器(HelloHandler)、一個服務器(HelloServer)、一個客戶程序(HelloClient)。
首先要做的是創建用於遠程過程調用的類和方法,人們常常稱之爲管理器。Xml-rpc管理器是一個方法和方法集,它接受xml-rpc請求,並對請求的內容進行解碼,再向一個類和方法發出請求。
//管理器類
  1. package xmlRpc;
  2.  
  3. /**
  4.  * @author trier
  5.  *
  6.  * <b><code>HelloHandler</code></b> is a simple handler than can 
  7.  *  be registered with an XML-RPC server
  8.  */
  9. public class HelloHandler {
  10.     public String sayHello(String name){
  11.         return "Hello " + name;
  12.     }
  13. }
服務器程序將創建的管理器註冊到服務器上,併爲服務器指明應用程序其他特定的參數。
//服務器類
  1. package xmlRpc;
  2.  
  3. /**
  4.  *
  5.  * <b><code>HelloServer</code></b> is a simple XML-RPC server
  6.  * that will take the <code>HelloHandler</code> class available
  7.  * for XML-PRC calls.
  8.  * 
  9.  */
  10. import org.apache.xmlrpc.WebServer;
  11. import org.apache.xmlrpc.XmlRpc;
  12. import java.io.IOException;
  13.  
  14. public class HelloServer {
  15.     public static void main(String[] args){
  16.         if(args.length<1){
  17.             System.out.println("Usage: java HelloServer [port]");
  18.             System.exit(-1);
  19.         }
  20.         try{
  21.             XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
  22.             //start the server
  23.             System.out.println("Starting XML-RPC Server......");
  24.             WebServer server = new WebServer(Integer.parseInt(args[0]));
  25.             //register our handler class
  26.             server.addHandler("hello",new HelloHandler());
  27.             System.out.println("Now accepting requests......");
  28.         }catch(ClassNotFoundException e){
  29.             System.out.println("Could not locate SAX Driver");
  30.         }catch(IOException e){
  31.             System.out.println("Could not start server: "+e.getMessage());
  32.         }
  33.     }
  34. }
//客戶程序
  1. package xmlRpc;
  2.  
  3. /**
  4. *
  5.   * <b><code>HelloClient</code></b> is a simple XML-RPC client
  6.  * that makes an XML-RPC request to <code>HelloServer</code>
  7.  */
  8. import java.io.IOException;
  9. import java.util.Vector;
  10.  
  11. import org.apache.xmlrpc.XmlRpc;
  12. import org.apache.xmlrpc.XmlRpcClient;
  13. import java.net.MalformedURLException;
  14. import org.apache.xmlrpc.XmlRpcException;
  15.  
  16. public class HelloClient {
  17.     public static void main(String[] args){
  18.         if(args.length<1){
  19.             System.out.println("Usage: java HelloClient [your name]");
  20.             System.exit(-1);
  21.         }
  22.         try{
  23.             //Use the Apache Xereces SAX Driver
  24.             XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
  25.             
  26.             //Specify the server
  27.             XmlRpcClient client = new XmlRpcClient("http://localhost:8585");
  28.             
  29.             //create request
  30.             Vector params = new Vector();
  31.             params.addElement(args[0]);
  32.             
  33.             //make a request and print the result
  34.             String result = (String)client.execute("hello.sayHello",params);
  35.             System.out.println("Response from server: "+ result);
  36.         }catch(ClassNotFoundException e){
  37.             System.out.println("Could not locate SAX Driver");
  38.         }catch(MalformedURLException e){
  39.             System.out.println("Incorrect URL fro xml-rpc server foramt:"+e.getMessage());
  40.         }catch(XmlRpcException e){
  41.             System.out.println("XmlRpcException :"+e.getMessage());    
  42.         }catch(IOException e){
  43.             System.out.println("IOException:"+e.getMessage());
  44.         }
  45.     }
  46. }

三、RPC和RMI的簡單比較


在RMI和RPC之間最主要的區別在於方法是如何別調用的。在RMI中,遠程接口使每個遠程方法都具有方法簽名。如果一個方法在服務器上執行,但是沒有相匹配的簽名被添加到這個遠程接口上,那麼這個新方法就不能被RMI客戶方所調用。在RPC中,當一個請求到達RPC服務器時,這個請求就包含了一個參數集和一個文本值,通常形成“classname.methodname”的形式。這就向RPC服務器表明,被請求的方法在爲“classname”的類中,名叫“methodname”。然後RPC服務器就去搜索與之相匹配的類和方法,並把它作爲那種方法參數類型的輸入。這裏的參數類型是與RPC請求中的類型是匹配的。一旦匹配成功,這個方法就被調用了,其結果被編碼後返回客戶方。

trier 整理:
參考資料:
  1. http://www.xmlrpc.com/
    《java&xml》O'Reilly
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章