Talking XMLRPC to a Wordpress instance

This is a short example demonstration how to use the Apache XMLRPC library to talk to a running Wordpress instance.

Wordpress 1.5 supports various XMLRPC operations by default ( MT & Blogger APIs for example ) however the two shown below are "demo" operations that all Wordpress installations should support and require no authentication.

To run this example you will need to include the dependencies - Apache XMLRPC & Jakarta COMMONS Codec - however if you are lazy, here is my "xmlrpc-2.0-dep.jar" JAR containing both.

Code

JAVA:
  1. import java.util.Vector;
  2.  
  3. import org.apache.xmlrpc.XmlRpcClient;
  4.  
  5. public class XMLRPCTest {
  6.  
  7.    /**
  8.     * @param args
  9.     */
  10.    public static void main(String[] args) {
  11.  
  12.       try {
  13.          
  14.          Object result;
  15.  
  16.          /* You can specify an alternative target URL here */
  17.          XmlRpcClient xmlrpc = new XmlRpcClient ("http://blog.lobstertechnology.com/xmlrpc");
  18.          
  19.          
  20.          /*
  21.           * demo.sayHello operation, returns a java.lang.String
  22.           *
  23.           */
  24.          result = xmlrpc.execute( "demo.sayHello", new Vector() );
  25.          System.out.println( "demo.sayHello: " + result +
  26.             " (" + result.getClass().getName() + ")" );
  27.  
  28.          
  29.          /*
  30.           * demo.addTwoNumbers operation, returns a java.lang.Integer
  31.           *
  32.           */
  33.          Vector params = new Vector ();
  34.          params.addElement ("5");
  35.          params.addElement ("8");
  36.          result = xmlrpc.execute( "demo.addTwoNumbers", params );
  37.          System.out.println( "demo.addTwoNumbers: " + result +
  38.             " (" + result.getClass().getName() + ")" );
  39.          
  40.       } catch ( Exception e ) {
  41.          e.printStackTrace();
  42.       }
  43.      
  44.    }
  45.  
  46. }

Example

CODE:
  1. demo.sayHello: Hello! (java.lang.String)
  2. demo.addTwoNumbers: 13 (java.lang.Integer)

Related Links

XMLRPCTest.java - Java Source Code
xmlrpc-2.0-dep.jar - Dependencies
Apache XMLRPC - http://ws.apache.org/xmlrpc/
Jakarta COMMONS Codec - http://jakarta.apache.org/commons/codec/