Q: How do I set HTTP headers in Axis clients?

A: Set the appropriate Hashtable entry in the MessageContext using a Handler:

package yourpackage;
import java.util.Hashtable;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.transport.http.HTTPConstants;
public class HTTPHeaderHandler extends BasicHandler {
  public void invoke(MessageContext ctx) throws AxisFault {
    Hashtable ht = (Hashtable)ctx.getProperty(HTTPConstants.REQUEST_HEADERS);
    if(ht == null) {
      ht = new Hashtable();
    // This will add an HTTP header with name "testheader" and a value of "this is a test"
    ht.put("testheader", "this is a test");
    ctx.setProperty(HTTPConstants.REQUEST_HEADERS, ht);

The next step is to configure the handler in client-config.wsdd:

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig" xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration>
  <parameter name="disablePrettyXML" value="true" />
  <parameter name="enableNamespacePrefixOptimization" value="false" />
 <handler type="java:yourpackage.HTTPHeaderHandler" name="HTTPHeaderHandler" />
 <transport name="http"
   <handler type="HTTPHeaderHandler" />
 <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender" />
 <transport name="java"  pivot="java:org.apache.axis.transport.java.JavaSender" />

You can find details of how to configure a Handler in AxisClientConfiguration and how to ensure your client code picks up your client-config.wsdd in AxisClientWSDD.

FrontPage/Axis/AxisClientSetHTTPHeaders (last edited 2010-09-16 00:56:58 by np45)