Calling Services with Apache xmlrpc client and Proxy

May 23, 2012

I needed to call a web service using Apache's xmlrpc client. Here's some example code for doing this:

try
{
  XmlRpcClientConfigImpl lConfig = new XmlRpcClientConfigImpl();
  lConfig.setServerURL(new URL(this.getUrlToCall()));

  XmlRpcClient lClient = new XmlRpcClient();
  lClient.setConfig(lConfig);

  Vector<String> lServiceParams = new Vector<String>();
  lServiceParams.addElement(lFirstParameter);
  lServiceParams.addElement(lSecondParameter);

  String lResponse = (String) lClient.execute("NameOfService", lServiceParams);
}
catch (XmlRpcException lXmlRpcException)
{
  throw new RuntimeException("Problems calling service, " + lXmlRpcException.getMessage(), lXmlRpcException);
}
catch (IOException lIoException)
{
  throw new RuntimeException("Problems calling service, " + lIoException.getMessage(), lIoException);
}

Proxy

To use this behind a proxy, use the standard java settings for setting the proxy host and port. If using Tomcat, these can be set in the JAVA_OPTS environment variable.

-Dhttp.proxyHost=hostname -Dhttp.proxyPort=1234

This assumes a proxy service available at hostname, on port 1234

For proxying https use:

-Dhttps.proxyHost=hostname -Dhttps.proxyPort=1234

You can also specify which domains are excluded from the proxy using http.nonproxyhosts

Tags: http proxy xmlrpc https