HOWTO: Using a Web Proxy with XML-RPC (Java)

| No TrackBacks
My team at work recently encountered an interesting problem that required us to configure an Apache XML-RPC client to use a web-proxy when communicating with a server.  In most corporate environments, the internal network sits behind a firewall and filtering web-proxy.  If your XML-RPC client needs to communicate with a server outside of the firewall, the only way out is through a web-proxy that supports XML-RPC.  After talking with several colleagues, I found that this seemingly simple need isn't very well documented.  So this post is an attempt to somewhat officially document how to use a web-proxy with Apache's XML-RPC Java implementation.  Surprisingly, it's actually quite simple once you've put the pieces together correctly.

If you're not familiar with XML-RPC, you can read up on it here.

Continue reading for the HOWTO.

The solution involves using an Apache Common's HttpClient with an XmlRpcCommonsTransportFactory.  In a nutshell, it's a matter of configuring your XML-RPC transport factory to use an Apache Common's HttpClient with the web-proxy set accordingly.

Step 1, set the XML-RPC server URL in an XmlRpcClientConfigImpl object.  Step 2, create a new XmlRpcClient object, and set the XmlRpcClientConfigImpl as its config.  Step 3, create a new XmlRpcCommonsTransportFactory and a new HttpClientStep 4, set the proxy you wish to use with the HttpClient using the setProxyHost() or setProxy() methods.  Step 5, call setHttpClient() on the transport factory to set the HTTP client to the one you just created with the proxy configuration.  Step 6, set the transport factory on the XmlRpcClient using setTransportFactory().

Here's some sample code ...

XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL( serverURL );

client = new XmlRpcClient();
client.setConfig( config );

XmlRpcCommonsTransportFactory transportFactory
= new XmlRpcCommonsTransportFactory( client );

HttpClient httpClient = new HttpClient();
HostConfiguration hostConfiguration = httpClient.getHostConfiguration();
hostConfiguration.setProxy( "proxy.example.com", 8088 );
hostConfiguration.setHost( serviceURL );

transportFactory.setHttpClient( httpClient );
client.setTransportFactory( transportFactory );

That's it!

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

No TrackBacks

No trackbacks attached to this entry.

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on December 17, 2008 9:35 PM.

HOWTO: Mount Samba (CIFS) Share on Linux (Symbolic Link Redirect) was the previous entry in this blog.

HOWTO: Recursively Delete Directories on Windows is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.