XmlRpc with C#/Java

4 篇文章 0 订阅

最近看了几个项目都是用xmlrpc协作完成的,就做了几个测试客户端和服务器端和大家一起分享。希望能对入门的同学有帮助

关于xmlrpc的介绍和规范参考http://www.xml-rpc.net/ 下面我就直奔主题举几个例子了

c#服务端

首先在VS中添加引用CookComputing.XmlRpc.dll

功能:仅仅返回一个拼接后的字符串

  1. using System;
  2. using CookComputing.XmlRpc;
  3. namespace xmlrpcServerTest
  4. {
  5.     public class server : XmlRpcService
  6.     {
  7.         [XmlRpcMethod("server.hello")]
  8.         public string hello(string param)
  9.         {
  10.             return "hello world "+param;
  11.         }
  12.     }
  13. }

在配置文件中加入如下配置(归属到<system.web>节点)

<httpHandlers>
  <add verb="*" path="server.aspx" type="xmlrpcServerTest.server, xmlrpcServerTest" />
</httpHandlers> 实现 web调用到应用程序类的映射

c#客户端 

  1. using System;
  2. using CookComputing.XmlRpc;
  3. namespace xmlrpcClientTest
  4. {
  5.     /// <summary>
  6.     /// Summary description for Class1.
  7.     /// </summary>
  8.     public interface client 
  9.     {
  10.         [XmlRpcMethod("server.hello")]
  11.         string hello(string param);
  12.     }
  13.     class xmlrpcClientTest
  14.     {
  15.         /// <summary>
  16.         /// The main entry point for the application.
  17.         /// </summary>
  18.         [STAThread]
  19.         static void Main(string[] args)
  20.         {
  21.             //
  22.             // TODO: Add code to start application here
  23.             //
  24.             client iclient;
  25.             XmlRpcClientProtocol protocol;
  26.             iclient = (client)XmlRpcProxyGen.Create(typeof(client));
  27.             protocol = (XmlRpcClientProtocol)iclient;
  28.             protocol.Url = "http://localhost/xmlrpcServerTest/server.aspx";
  29.             protocol.KeepAlive = false;
  30.             string ret = iclient.hello("test"); //调用
  31.             Console.WriteLine(ret);
  32.             Console.ReadLine();
  33.         }
  34.     }
  35. }

其中客户端的web方法名称即[ ]中的名称必须和服务端相同,否则会抛异常。

再看java:

java服务端:

首先在你所使用的IDE中导入xmlrpc组件的包

rpc代码如下、web处理部分可以用servlet或jsp来调用这个类、封装到一个方法中

 

  1. //传入request和response内置对象 
  2. .........
  3. public class server {  
  4.   public void invoke() {
  5.   XmlRpcServer xmlrpc = new XmlRpcServer();
  6.     xmlrpc.addHandler("server"new serverImpl());
  7.     byte[] result = xmlrpc.execute(request.getInputStream());   
  8. ..........
  9.   }
  10. }
  11. ..........
  12. public class serverImpl{
  13.     String function(String paramHead,String paramTail){
  14.         return paramHead+paramTail;
  15.     }
  16. }

serverImpl为一个普通的java类,可以用来处理业务逻辑,“server

java客户端:

  1. try{
  2.             XmlRpcClient client = new XmlRpcClient(http://localhost:8080/project/servProvider);
  3.                                                    //project是你的工程名字
  4.                                                    //servProvider可以是servlet或jsp 
  5.             String[] param = {"hello ","world"};
  6.             Vector param_vector = new Vector();
  7.             param_vector.addElement(param[0]);
  8.             param_vector.addElement(param[1]);                   
  9.             String res ="sdf";
  10.             res = (String)client.execute("server.function",param_vector);
  11.             System.out.println(res);
  12.         }
  13.         catch(MalformedURLException e)
  14.         {
  15.             System.out.println(e.toString());
  16.         }
  17.         catch (IOException e) {
  18.             System.out.println(e.toString());
  19.         }
  20.         catch (XmlRpcException e) {
  21.             System.out.println(e.toString());
  22.         }

完毕,这就是很有用的xmlrpc

”可以理解为serverImpl的一个代理或标号,便于服务器端定向

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值