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. <?xml version="1.0"?>
  9. <methodCall>
  10.    <methodName>examples.getStateName</methodName>
  11.    <params>
  12.       <param>
  13.          <value><i4>41</i4></value>
  14.          </param>
  15.       </params>
  16.    </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. <?xml version="1.0"?>
  10. <methodResponse>
  11.    <params>
  12.       <param>
  13.          <value><string>South Dakota</string></value>
  14.          </param>
  15.       </params>
  16.    </methodResponse>

二、xml-rpc入门程序


以下的入门程序包括一个管理器(HelloHandler)、一个服务器(HelloServer)、一个客户程序(HelloClient)。
首先要做的是创建用于远程过程调用的类和方法,人们常常称之为管理器。Xml-rpc管理器是一个方法和方法集,它接受xml-rpc请求,并对请求的内容进行解码,再向一个类和方法发出请求。
//管理器类
  1. package xmlRpc;

  2. /**
  3.  * @author trier
  4.  *
  5.  * <b><code>HelloHandler</code></b> is a simple handler than can 
  6.  *  be registered with an XML-RPC server
  7.  */
  8. public class HelloHandler {
  9.     public String sayHello(String name){
  10.         return "Hello " + name;
  11.     }
  12. }
服务器程序将创建的管理器注册到服务器上,并为服务器指明应用程序其他特定的参数。
//服务器类
  1. package xmlRpc;

  2. /**
  3.  *
  4.  * <b><code>HelloServer</code></b> is a simple XML-RPC server
  5.  * that will take the <code>HelloHandler</code> class available
  6.  * for XML-PRC calls.
  7.  * 
  8.  */
  9. import org.apache.xmlrpc.WebServer;
  10. import org.apache.xmlrpc.XmlRpc;
  11. import java.io.IOException;

  12. public class HelloServer {
  13.     public static void main(String[] args){
  14.         if(args.length<1){
  15.             System.out.println("Usage: java HelloServer [port]");
  16.             System.exit(-1);
  17.         }
  18.         try{
  19.             XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
  20.             //start the server
  21.             System.out.println("Starting XML-RPC Server......");
  22.             WebServer server = new WebServer(Integer.parseInt(args[0]));
  23.             //register our handler class
  24.             server.addHandler("hello",new HelloHandler());
  25.             System.out.println("Now accepting requests......");
  26.         }catch(ClassNotFoundException e){
  27.             System.out.println("Could not locate SAX Driver");
  28.         }catch(IOException e){
  29.             System.out.println("Could not start server: "+e.getMessage());
  30.         }
  31.     }
  32. }
//客户程序
  1. package xmlRpc;

  2. /**
  3. *
  4.   * <b><code>HelloClient</code></b> is a simple XML-RPC client
  5.  * that makes an XML-RPC request to <code>HelloServer</code>
  6.  */
  7. import java.io.IOException;
  8. import java.util.Vector;

  9. import org.apache.xmlrpc.XmlRpc;
  10. import org.apache.xmlrpc.XmlRpcClient;
  11. import java.net.MalformedURLException;
  12. import org.apache.xmlrpc.XmlRpcException;

  13. public class HelloClient {
  14.     public static void main(String[] args){
  15.         if(args.length<1){
  16.             System.out.println("Usage: java HelloClient [your name]");
  17.             System.exit(-1);
  18.         }
  19.         try{
  20.             //Use the Apache Xereces SAX Driver
  21.             XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser");
  22.             
  23.             //Specify the server
  24.             XmlRpcClient client = new XmlRpcClient("http://localhost:8585");
  25.             
  26.             //create request
  27.             Vector params = new Vector();
  28.             params.addElement(args[0]);
  29.             
  30.             //make a request and print the result
  31.             String result = (String)client.execute("hello.sayHello",params);
  32.             System.out.println("Response from server: "+ result);
  33.         }catch(ClassNotFoundException e){
  34.             System.out.println("Could not locate SAX Driver");
  35.         }catch(MalformedURLException e){
  36.             System.out.println("Incorrect URL fro xml-rpc server foramt:"+e.getMessage());
  37.         }catch(XmlRpcException e){
  38.             System.out.println("XmlRpcException :"+e.getMessage());    
  39.         }catch(IOException e){
  40.             System.out.println("IOException:"+e.getMessage());
  41.         }
  42.     }
  43. }

三、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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值