how to add a http header to a soaprequest in java

tgr picture tgr · Aug 22, 2012 · Viewed 20.5k times · Source

I try to connect to a Yahoo webservice. I generated the classes by axis2. The problem I am facing right now is, that the webservice requires a specific key value pair in the header and I am absolutely not able, to do so. I searched the web and found different possibilities - none of them worked for me. The most promissing was the post nearly at the end of this page, were Claude Coulombe sugested to change the code of the generated stub, but this failed as well. Can anybody show me a way how to solve this issue?

Edit

The suggested way using Options produced the following exception:

Exception in thread "main" org.apache.axis2.AxisFault: Address information does not exist in the Endpoint Reference (EPR).The system cannot infer the transport mechanism.

Here is my code:

val stub = new IndexToolsApiServiceStub("https://api.web.analytics.yahoo.com/IndexTools/services/IndexToolsApiV3")

val client = stub._getServiceClient
val options = new Options
val list = new ArrayList[Header]()
val header = new Header
header.setName("YWA_API_TOKEN")
header.setValue("NOTtheREALvalue")
list.add(header)
options.setProperty(HTTPConstants.HTTP_HEADERS, list)
client.setOptions(options)
stub._setServiceClient(client)

Answer

davidfmatheson picture davidfmatheson · Aug 22, 2012

You probably want to use Axis2's Options:

// Create an instance of org.apache.axis2.client.ServiceClient
ServiceClient client = ...

// Create an instance of org.apache.axis2.client.Options
Options options = new Options();

List list = new ArrayList();

// Create an instance of org.apache.commons.httpclient.Header
Header header = new Header();

// Http header. Name : user, Value : admin
header.setName("user");
header.setValue("admin");

list.add(header);
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, list);

client.setOptions(options);

Here's the reference for that code.