XML-RPC 之 Apache XML-RPC 实例


<style type="text/css"> pre.programlisting { font-family: Courier, Lucida Console; font-size: 100%; margin-left: 0px; margin-right: 0px; margin-top: 5px; margin-bottom: 5px; border: #164781 1px solid; padding: 10px; color: #003366; background: #c2d1e7 none } </style>
作者:王恩建 来源:http://www.sentom.net

XML-RPC 是工作在 Internet 上的远程过程调用协议。通俗点讲,就是使用 HTTP 协议交互,交互的载体是 XML 文件。XML-RPC 具体的规范说 明请参考 这里


图片来自XML-RPC官方网站

XML-RPC 规范定义了六种数据类型,下表是这六种数据类型与 Java 的数据类型对应表。

XML-RPC Java
<i4> 或 <int> int
<boolean> boolean
<string> java.lang.String
<double> double
<dateTime.iso8601> java.util.Date
<struct> java.util.Hashtable
<array> java.util.Vector
<base64> byte[ ]

XML-RPC 规范的各种平台都有具体实现,XML-RPC 规范的 Java 实现都有好几种,这里我们选择了 Apache XML-RPC

XML-RPC 服务端实现
先定义一个简单业务对象 MyHandler,远程客户端将调用该对象的方法,具体代码如下:

package net.sentom.xmlrpc;

public class MyHandler {
	
	public String sayHello(String str){
		return "Hello," + str;
	}
}

然后定义一个 Servlet 名叫 MyXmlRpcServer,远程客户端通过 HTTP-POST 访问该 Servlet。

package net.sentom.xmlrpc;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.xmlrpc.XmlRpcServer;

public class MyXmlRpcServer extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		XmlRpcServer xmlrpc = new XmlRpcServer();
		xmlrpc.addHandler("myHandler", new MyHandler());
		byte[] result = xmlrpc.execute(request.getInputStream());
		response.setContentType("text/xml");
		response.setContentLength(result.length);
		OutputStream out = response.getOutputStream();
		out.write(result);
		out.flush();
	}
}

需要特别说明是:

xmlrpc.addHandler("myHandler", new MyHandler());

为了便于理解,这里可以看成普通的:

MyHandler myHandler = new MyHandler();

最后在web.xml文件中加入以下几行:

<servlet>
    	<servlet-name>MyXmlRpcServer</servlet-name>
    	<servlet-class>net.sentom.xmlrpc.MyXmlRpcServer</servlet-class>
</servlet>
<servlet-mapping>
    	<servlet-name>MyXmlRpcServer</servlet-name>
    	<url-pattern>/MyXmlRpcServer</url-pattern>
</servlet-mapping>
XML-RPC 客户端实现
客户端相对简单一些,先来一个 Java 客户端实现 MyXmlRpcClient:

package net.sentom.xmlrpc;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Vector;
import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

public class MyXmlRpcClient {
	public static void main(String[] args) {
		try {
			XmlRpcClient xmlrpc = new XmlRpcClient("http://localhost:8080/XMLRPC/MyXmlRpcServer");
			Vector params = new Vector();
			params.addElement("Tom");
			String result = (String) xmlrpc.execute("myHandler.sayHello",params);
			System.out.println(result);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (XmlRpcException e) {
			System.out.println(e.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

http://localhost:8080/XMLRPC/MyXmlRpcServer 为 MyXmlRpcServer 的访问URL。

String result = (String) xmlrpc.execute("myHandler.sayHello",params);

再来一个 Python 客户端实现

import xmlrpclib
url = 'http://localhost:8080/XMLRPC/MyXmlRpcServer';
server = xmlrpclib.Server(url);
print server.myHandler.sayHello('Tom');
<!-- google_ad_client = "pub-9232855773311077"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_channel =""; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "008000"; google_color_text = "000000"; //--> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9232855773311077&amp;dt=1112239954046&amp;format=300x250_as&amp;output=html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=FFFFFF&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=8&amp;u_java=true" frameborder="0" width="300" scrolling="no" height="250" allowtransparency="65535"></iframe>

你可能感兴趣的:(java,apache,xml,servlet,Google)