Hi Ryan,
here are my examples. They're mostly ripped straight out of the main methods of existing stuff.
Sorry not to tidy them up more, but its 1am, and I'm supposed to be doing something else.
Feel free to use them, modify them, or throw them away. I'm going to do a secure version, maybe tomorrow, in the hope that someone will give me some pointers on how to get it working :-)
import java.util.Vector; import java.io.IOException; import java.lang.NumberFormatException;
import org.apache.xmlrpc.WebServer;
import org.apache.xmlrpc.XmlRpcHandler;
/**
* This is a class for testing xmlrpc.
*
* It only has one function, which returns the value 42.
*
* Usage: java TestServer <port>
*
*/
public class TestServer implements XmlRpcHandler{
/**
* @param method The name of the method that has been invoked.
* @param params The parameters of the method call.
* @return An object containing the results of the method call.
* @throws An Exception if there are any problems.
*/
public Object execute(String method, Vector params) throws Exception{
System.err.println("TestServer: executing " + method);
Integer result = new Integer(42);
return result;
}
/**
* Launches a server, to listen on port XX.
*
* @param args The supplied command line arguments.
*/
public static void main(String args[]){
int port = 8080;
if(args.length > 0){
try{
port = Integer.parseInt(args[0]);
}
catch(NumberFormatException e){
e.printStackTrace();
}
}
System.out.println("Starting XML-RPC TestServer on port " + port);
WebServer webServer = null;
try{
webServer = new WebServer(port);
}
catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
TestServer testServer = new TestServer();
webServer.addHandler("test", testServer);
}
}
import java.util.Vector;
import org.apache.xmlrpc.XmlRpcClient;
/**
* A very basic XML-RPC client.
*
* Usage: java org.apache.xmlrpc.TestClient <url> <method> <arg>
*
*/
public class TestClient {
public final static String DEFAULT_URL_STRING = "http://127.0.0.1:8080/RPC2";
public final static String DEFAULT_METHOD = "test.testmethod";
/**
* Makes a request to the test server, and prints the results to stderr.
*/
public static void main(String args[]) throws Exception{
try{
String url = DEFAULT_URL_STRING;
if(args.length >= 1) url = args[0];
String method = DEFAULT_METHOD;
if(args.length >= 2) method = args[1];
Vector v = new Vector();
for (int i = 2; i < args.length; i++){
try{
v.addElement(new Integer(Integer.parseInt(args[i])));
}
catch(NumberFormatException nfx){
v.addElement(args[i]);
}
}
XmlRpcClient client = new XmlRpcClient(url);
try{
System.err.println(client.execute(method, v));
}
catch(Exception ex){
System.err.println("Error: " + ex.getMessage());
}
}
catch(Exception x){
System.err.println(x);
System.err.println("Usage: java TestClient <url> <method> <arg> ....");
System.err.println("Arguments are sent as integers or strings.");
}
}
}
Assuming that you have xmlrpc-1.1.jar in your current directory: to compile: javac -classpath xmlrpc-1.1.jar *.java to run: java -classpath xmlrpc-1.1.jar:. TestServer & then java -classpath xmlrpc-1.1.jar:. TestClient & You should see the following: % % java -classpath ../xmlrpc-1.1/xmlrpc-1.1.jar:. TestServer & Starting XML-RPC TestServer on port 8080 % java -classpath ../xmlrpc-1.1/xmlrpc-1.1.jar:. TestClient TestServer: executing testmethod 42 %
On Tuesday, October 29, 2002, at 12:32 AM, Ryan Hoegg wrote:
I just went poking around my sources and found a good example of both client and server under the examples/echo directory. Unfortunately it is not included in the xmlrpc-1.1-src.zip on the site :(
I will look around some more and see what I can find, but you can get it from anonymous CVS.
--
Ryan Hoegg
ISIS Networks
http://www.isisnetworks.net
sean wrote:
What would be nice is if you did away with the server example that's there now entirely and put up a "Hello, world" application (complete source, nothing fancy) with full instructions on getting the example working so anyone new who comes along can follow the instructions, get the server working, then contact the server with a browser (like Netscape) and see the results.
