简单的java xmlrpc



简单的 XMLRPC

java---XmlRpcClient


package com.company;

import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

import java.net.MalformedURLException;
import java.util.Hashtable;
import java.util.Vector;

public class Main {

    private final static String server_url = "http://localhost:8080/RPC2";

    public static void main(String[] args) {
	// write your code here
        // Create an object to represent our server.
        XmlRpcClient server = null;
        try {
            server = new XmlRpcClient(server_url);
            // Build our parameter list.
            Vector params = new Vector();
            params.addElement(new Integer(7));
            params.addElement(new Integer(2));
// Call the server, and get our result.
              Hashtable result = (Hashtable) server.execute("cals.sub", params);
              int sum = ((Integer) result.get("sub")).intValue();
// Print out our result.
              System.out.println("Sum: " + Integer.toString(sum));

        } catch (org.apache.xmlrpc.XmlRpcException exception) {
            System.err.println("JavaClient: XML-RPC Fault #" +
                    Integer.toString(exception.code) + ": " + exception.toString());
        } catch (Exception exception) {
            System.err.println("JavaClient: " + exception.toString());
        }

       // } catch (MalformedURLException e) {
       //     e.printStackTrace();
        //}

    }
}


Java xmlRpc-Server

package com.company;
import java.util.Hashtable;

import org.apache.xmlrpc.*;

public class Main {
    public Main(){
      System.out.println("Handler registered as 'cals'");
    }
    public Hashtable add(int x ,int y){
        Hashtable result = new Hashtable();
        result.put("add",new Integer(x+y));
        return  result;
    }

    public  Hashtable sub(int x ,int y){
        Hashtable result = new Hashtable();
        result.put("sub",new Integer(x-y));
        return result;
    }

    public Hashtable div(int x,int y){
        Hashtable result = new Hashtable();
        if( y == 0){
            result.put("div error ",new Integer(0));
        }else {
            result.put("div ",new Integer(x/y));
        }
        return  result;
    }

    public Hashtable mul(int x,int y){
        Hashtable result = new Hashtable();
        result.put("mul",new Integer(x*y));
        return  result;
    }

    public static void main(String[] args) {
        // write your code here
        try {
            WebServer server = new WebServer(8080);
            server.addHandler("cals", new Main());
            server.start();
        }
        catch(Exception exception){
                System.err.println("JavaServer: " + exception.toString());
            }
        }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值