Search This Blog

Friday, March 11, 2011

Custom HTTP headers with XmlRpcClient

Recently I encountered the problem with apache xml-rpc client when I needed to set some custom request headers with each request. Since I didn't find any examples on google how to do it here is an example.

For this to work you need to implement XmlRpcTransport and override initHttpHeaders method.

XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL("http", "localhost", 9000, ""));
        client = new XmlRpcClient();
        //implement transport factory
        XmlRpcTransportFactory xmlRpcTransportFactory = new XmlRpcCommonsTransportFactory(client) {

            public XmlRpcTransport getTransport() {

                return new XmlRpcCommonsTransport(this) {

                    protected void initHttpHeaders(XmlRpcRequest pRequest) throws XmlRpcClientException {
                        //add custom header
                        super.method.addRequestHeader("My-Header", "some header");
        factory = new ClientFactory(client);
        //actual method call
        IMyInterface myHandler = (IMyInterface)factory.newInstance(IMyInterface.class);
For every remote method call, header named My-header will be set to http request.


  1. great, thank you.

  2. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome. You can also check my articles as well.

    Admissiongyan, the best German overseas education consultant in Bangalore, since 2012 for study abroad incl. Germany, France, Italy, Sweden, Ireland, NL, or UK.
    best overseas consultants in bangalore
    ms in mechanical engineering in germany
    Masters in Mechanical Engineering in Germany
    free education in germany
    mba in germany
    ms in germany for indian students
    study ms in germany
    study in germany consultants