XML-RPC的基本描述

XML-RPC的全称是XML Remote Procedure Call,即XML标准通用标记语言下的一个子集)远程方法调用。它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范和一系列的实现。这种远程过程调用使用http作为传输协议,XML作为传送信息的编码格式。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
<?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入门程序

基本做法

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

管理器类

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;
}
}

服务器程序将创建的管理器注册到服务器上,并为服务器指明应用程序其他特定的 参数

服务器类

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.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.i.IOException;
import java.util.Vector;
import org.apache.xmlrpc.XmlRpc;
import org.apache.xmlrpc.XmlRpcclient;
import java.t.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());
}
}
}

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念