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, ""));
               
        config.setEnabledForExtensions(true);
        config.setContentLengthOptional(false);
        
        client = new XmlRpcClient();
        client.setConfig(config);
        //implement transport factory
        XmlRpcTransportFactory xmlRpcTransportFactory = new XmlRpcCommonsTransportFactory(client) {

            @Override
            public XmlRpcTransport getTransport() {

                return new XmlRpcCommonsTransport(this) {

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

2 comments:

  1. great, thank you.

    ReplyDelete
  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

    ReplyDelete