简单的 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());
}
}
}