本文转自http://blog.csdn.net/luckyspring/archive/2007/05/23/1623374.aspx
过程:在tomcat中的web.xml增加如下配置:
<servlet> <servlet-name>SampleServiceServlet</servlet-name> <servlet-class>test4.ServletXmlRPCServer</servlet-class> </servlet> <servlet-mapping> <servlet-name>SampleServiceServlet</servlet-name> <url-pattern>/SampleServiceServlet</url-pattern> </servlet-mapping>
参考类:附件中的test4.SampleService,test4.ServletXmlRPCServer,test4.XmlRPCClient
//********************************* begin test.XmlRPCClient ************************************// package test; import java.util.*; import org.apache.xmlrpc.*; public class XmlRPCClient { // The location of our server. private final static String server_url = "http://localhost:9001"; public static void main(String[] args) throws Exception{ XmlRpc.debug = true; // Create an object to represent our server. XmlRpcClient server = new XmlRpcClient(server_url); // Build our parameter list. Vector params = new Vector(); params.addElement(new Integer(5)); params.addElement(new Integer(3)); // Call the server, and get our result. Hashtable result = (Hashtable) server.execute("mysample.sumAndDifference", params); int sum = ( (Integer) result.get("sum")).intValue(); int difference = ( (Integer) result.get("difference")).intValue(); System.out.println("Sum: " + Integer.toString(sum) + ", Difference: " + Integer.toString(difference)); } } //********************************* end test.XmlRPCClient ************************************//
//********************************** begin test.JavaServer ************************************// package test; import java.util.*; import org.apache.xmlrpc.WebServer; public class JavaServer { public JavaServer() { } public Hashtable sumAndDifference(int x, int y) { Hashtable result = new Hashtable(); result.put("sum", new Integer(x + y)); result.put("difference", new Integer(x - y)); return result; } public static void main(String[] args) { try { // Invoke me as <http://localhost:9001/RPC2>. WebServer webserver = new WebServer(9001); webserver.addHandler("mysample", new JavaServer()); webserver.start(); } catch (Exception exception) { System.err.println("JavaServer: " + exception.toString()); } } } //********************************** end test.JavaServer ************************************//
//********************************** begin test2.XmlRPCClient ************************************// package test2; import java.util.*; import org.apache.xmlrpc.*; public class XmlRPCClient { // The location of our server. private final static String server_url = "http://localhost:9001"; public static void main(String[] args) { try { XmlRpc.debug = true; XmlRpcClient server = new XmlRpcClient(server_url); // Build our parameter list. Vector params = new Vector(); Vector arg0 = new Vector(); arg0.add("army"); arg0.add("robin"); params.addElement(arg0); Hashtable arg1 = new Hashtable(); arg1.put("a","a"); arg1.put("b","b"); params.addElement(arg1); Integer arg2 = new Integer(3); params.addElement(arg2); Double arg3 = new Double(11.33); params.addElement(arg3); Boolean arg4 = new Boolean(false); params.addElement(arg4); Date arg5 = Calendar.getInstance().getTime(); params.addElement(arg5); // Call the server, and get our result. Object ret = server.execute("sample.getAllUser",params); Vector result = (Vector)ret; if(result!=null){ System.out.println("result:" + result.size() + "records"); for(int i=0;i<result.size();i++){ System.out.println("result " + i + "is:" + result.get(i)); } } } catch (XmlRpcException exception) { System.err.println("XmlRPCClient: XML-RPC Fault #" + Integer.toString(exception.code) + ": " + exception.toString()); } catch (Exception exception) { exception.printStackTrace(System.err); } } } //*********************** end test2.XmlRPCClient *********************************//
//*********************** begin test2.JavaServer*********************************// package test2; import java.util.*; import org.apache.xmlrpc.WebServer; public class JavaServer { public JavaServer () { // Our handler is a regular Java object. It can have a // constructor and member variables in the ordinary fashion. // Public methods will be exposed to XML-RPC clients. } public Vector getAllUser (Vector arg0,Hashtable arg1,int arg2,double arg3,boolean arg4,Date arg5) { if(arg0!=null){ System.out.println(arg0.size()); } if(arg1!=null){ System.out.println(arg1.size()); } System.out.println(arg2); System.out.println(arg3); System.out.println(arg4); System.out.println(arg5); Vector result = new Vector(); result.add("bruce"); result.add("测试中文"); result.add("orbison"); return result; } public static void main (String [] args) { try { // Invoke me as <http://localhost:9001/RPC2>. WebServer webserver = new WebServer(9001); webserver.addHandler("sample", new JavaServer()); webserver.start(); } catch (Exception exception) { System.err.println("JavaServer: " + exception.toString()); } } } //*********************** end test2.JavaServer *********************************//
//********************* begin test3.XmlRPCSerializable ****************************// package test3; import java.util.Vector; public interface XmlRPCSerializable { /*将本对象序列化为Vector对象*/ public Vector serialize(); /*从v中反序列化本对象*/ public void deSerialize(Vector v); } //******************** end test3.XmlRPCSerializable **********************//
//******************** begin test3.AccountVO *********************// package test3; import java.util.*; public class AccountVO implements XmlRPCSerializable{ private String acctId; private double balance; public AccountVO() { } public String toString(){ return "acctId:" + acctId + ",balance:" + balance; } public Vector serialize(){ Vector v = new Vector(); v.add(acctId); v.add(new Double(balance)); return v; } public void deSerialize(Vector v){ acctId = (String)v.get(0); balance = ((Double)v.get(1)).doubleValue(); } public String getAcctId() { return acctId; } public void setAcctId(String acctId) { this.acctId = acctId; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } //************************ end test3.AccountVO *************************//
//************************ begin test3.XmlRPCClient*************************// package test3; import java.util.*; import org.apache.xmlrpc.*; public class XmlRPCClient { // The location of our server. private final static String server_url = "http://localhost:9001"; public static void main(String[] args) { try { XmlRpc.debug = false; // Create an object to represent our server. XmlRpcClient server = new XmlRpcClient(server_url); // Build our parameter list. Vector params = new Vector(); // Call the server, and get our result. Vector result = (Vector) server.execute("mysample.getAllAccountVO", params); if (result != null) { for (int i = 0; i < result.size(); i++) { Vector v = (Vector) result.get(i); AccountVO vo = new AccountVO(); vo.deSerialize(v); System.out.println(vo); } } } catch (XmlRpcException exception) { System.err.println("XmlRPCClient: XML-RPC Fault #" + Integer.toString(exception.code) + ": " + exception.toString()); } catch (Exception exception) { exception.printStackTrace(); } } } //************************ end test3.XmlRPCClient*************************//
//************************ begin test3.JavaServer*************************// package test3; import java.util.*; import org.apache.xmlrpc.WebServer; public class JavaServer { public JavaServer () { } public Vector getAllAccountVO () { Vector result = new Vector(); AccountVO vo = new AccountVO(); vo.setAcctId("robin"); vo.setBalance(300000.00); result.add(vo.serialize()); vo = new AccountVO(); vo.setAcctId("amy"); vo.setBalance(100000.10); result.add(vo.serialize()); return result; } public static void main (String [] args) { try { // Invoke me as <http://localhost:9001/RPC2>. WebServer webserver = new WebServer(9001); webserver.setParanoid (true); // deny all clients webserver.acceptClient ("192.168.0.*"); // allow local access webserver.denyClient ("192.168.0.3"); // except for this one webserver.setParanoid (false); // disable client filter webserver.addHandler("sample", new JavaServer()); webserver.addHandler("mysample",new JavaServer()); webserver.start(); } catch (Exception exception) { System.err.println("JavaServer: " + exception.toString()); } } } ////////////////////////end test3.JavaServer////////////////////////////////// //************************ end test3.JavaServer*************************//
//************************ begin test4.SampleService*************************// package test4; import java.util.*; public class SampleService { public SampleService() { } public Date currentTime(){ return Calendar.getInstance().getTime(); } } ////////////////////////end test4.SampleService////////////////////////////////// ////////////////////////begin test4.ServletXmlRPCServer////////////////////////////////// package test4; import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.*; import org.apache.xmlrpc.*; public class ServletXmlRPCServer extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { XmlRpcServer xmlrpc = new XmlRpcServer(); xmlrpc.addHandler("sampleService", new SampleService()); byte[] result = xmlrpc.execute(request.getInputStream()); response.setContentType("text/xml"); response.setContentLength(result.length); OutputStream out = response.getOutputStream(); out.write(result); out.flush(); } } ////////////////////////end test4.ServletXmlRPCServer////////////////////////////////// //*********************** end test4.ServletXmlRPCServer **************************//
//*********************** begin test4.XmlRPCClient**************************// package test4; import java.util.*; import org.apache.xmlrpc.*; public class XmlRPCClient { // The location of our server. private final static String server_url = "http://localhost:8080/axis/SampleServiceServlet"; public static void main (String [] args) { try { XmlRpc.debug = false; // Create an object to represent our server. XmlRpcClient server = new XmlRpcClient(server_url); // Build our parameter list. Vector params = new Vector(); // Call the server, and get our result. Date result = (Date) server.execute("sampleService.currentTime", params); System.out.println(result.getTime()); } catch (XmlRpcException exception) { System.err.println("XmlRPCClient: XML-RPC Fault #" + Integer.toString(exception.code) + ": " + exception.toString()); } catch (Exception exception) { System.err.println("XmlRPCClient: " + exception.toString()); } } } //*********************** end test4.XmlRPCClient**************************//
=======================================================================================
本文转自http://www.cnblogs.com/chenxizhang/archive/2008/08/23/1274499.html
因为最近研究博客系统,对XML RPC的机制有了更深入的一些了解。下面总结一下
1. 什么是XML -RPC?为什么需要它?
有关详细的介绍,你可以参考下面的说明。简单来说,XML-RPC是一个简单的协议和标准,它通过标准的HTTP请求和响应来实现,数据是以xml格式传递的。它的优点是跨平台。如果仅仅在.NET平台,那么它也许并不是一个最好的选择,而另外一种技术SOAP Web Service可能更加好一些
http://www.xml-rpc.net/faq/xmlrpcnetfaq.html
To quote the XML-RPC.com site:
"It's a spec and a set of implementations that allow software running on disparate operating systems, running in different environments to make procedure calls over the Internet. It's remote procedure calling using HTTP as the transport and XML as the encoding. XML-RPC is designed to be as simple as possible, while allowing complex data structures to be transmitted, processed and returned.";
XML-RPC.NET is a .NET class library for implementing XML-RPC clients and servers.
If your clients and servers are all running in the .NET environment there is no point in using XML-RPC: .NET provides excellent support for SOAP and XML-RPC doesn't have any features not provided by SOAP (other than simplicity).
If you use .NET clients and want to connect to XML-RPC servers running under any OS then XML-RPC.NET is a good choice.
If you want to implement a server in the .NET environment which is to be connected to by clients running in other environments, say Unix or Java, then XML-RPC may be an appropriate choice. SOAP is supported in many different environments but is considerably more complicated than XML-RPC and presents more opportunity for interop problems
我们在这里要感谢Cook这个人,是他将XML-RPC进一步标准化,目前我们大部分人使用的XML-RPC组件,都叫做CookComputing.XmlRpc
这里还有一个介绍(中文的)http://www.mengyan.org/blog/archives/2005/07/12/30.html
下面有一篇文章很好地介绍了该过程 http://nayyeri.net/blog/implement-metaweblog-api-in-asp-net/
典型的博客客户端都实现了该协议。例如Windows Live Writer等等。下面三个图片就展示了如何在WLW中添加一个博客账户的界面。甚至连Word 2007也实现了该功能。
抛开博客系统,如果我们需要在分布式开发中使用XML-RPC技术,那么该怎么办呢?
using CookComputing.XmlRpc; namespace interfaces { public interface IMath { [XmlRpcMethod("Imath.add")] [return:XmlRpcReturnValue(Description="返回两个值加法的结果")] int Add( [XmlRpcParameter(Description="第一个参数")] int a, [XmlRpcParameter(Description="第二个参数")] int b); } }
using CookComputing.XmlRpc; namespace Services { [XmlRpcService(AutoDocumentation=true,Description="这是我的服务")] public class MathService:XmlRpcService,interfaces.IMath { #region IMath 成员 int interfaces.IMath.Add(int a, int b) { return a + b; } #endregion } }
这里除了实现接口,不要忘记继承XmlRpcService这个类
我们这里简单地把该服务宿主在一个网站里面,那么修改该web.config文件
<httpHandlers> <add verb="*" type="Services.MathService" path="MathService.axd"/> </httpHandlers>
using System; using CookComputing.XmlRpc; namespace Client { class Program { static void Main(string[] args) { string url = "http://localhost:1500/MathService.axd"; IMath mathservice = XmlRpcProxyGen.Create<IMath>(); mathservice.Url = url; Console.WriteLine(mathservice.Add(100, 200)); Console.Read(); } } public interface IMath:IXmlRpcProxy { [XmlRpcMethod("Imath.add")] [return: XmlRpcReturnValue(Description = "返回两个值加法的结果")] int Add( [XmlRpcParameter(Description = "第一个参数")] int a, [XmlRpcParameter(Description = "第二个参数")] int b); } } 这里有一个关键,就是在客户程序中的那个接口要实现IXmlRpcProxy,否则是无法指定Url的。
这个客户端代理接口中的XmlRpcMethod必须与服务端是对应的