how to add a http header to a soaprequest in java
Asked Answered
S

6

10

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)
Stines answered 22/8, 2012 at 11:10 Comment(1)
I assume your question is about HTTP headers (not HTML). You may want to correct the typo...Ric
S
0

I found a solution for the problem two months ago. You are not able to set a customized header with Axis2. So I fell back to the older Axisversion, where you can do it. Setting the Http-header by yourself is no good practice and mostly unneccesary. On top it is not a part of the SOAP-specification. That is the reason why you cannot do it with Axis2.

Stines answered 8/1, 2013 at 7:19 Comment(0)
C
8

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.

Cohlette answered 22/8, 2012 at 12:37 Comment(0)
S
3

It doesn't matter if you want to add HTTP headers to your SOAP request or response. either ways you should work with MessageContext. Supposed that msgContext is your Axis2 request/response Message Context object (org.apache.axis2.context.MessageContext), below code will do the trick and using it, you can add HTTP headers.

`//Instantiate an Options object from org.apache.axis2.client.Options
 Options options = new Options();
 //Instantiate an ArrayList of type NamedValue from org.apache.axis2.context.NamedValue
 List<NamedValue> namedValuePairs = new ArrayList<NamedValue>();
 //Add as much as headers you want using below code
 namedValuePairs.add(new NamedValue("sample", "value"));
 //Finally add namedValuePairs to options, and add options to msgContext
 options.setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, namedValuePairs);
 msgContext.setOptions(options);`
Shindig answered 21/5, 2016 at 21:58 Comment(0)
E
2

Actually, you just have to retrieve the options reference from the ServiceClient instead of replacing the options object. Then add the properties you want:

ServiceClient sc = awst._getServiceClient();
Options ops = sc.getOptions();
Essence answered 27/2, 2013 at 16:24 Comment(0)
B
1

The solution -

    MyStub stub = new MyStub();
    ServiceClient serviceClient = stub._getServiceClient();
    Options options = serviceClient.getOptions();
    List<NamedValue> namedValuePairs = new ArrayList<NamedValue>();
    namedValuePairs.add(new NamedValue("Authorization", "Basic JSDFANSKMSLAWQEINCCAKNASKNAS2371BASCKA="));
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, namedValuePairs);

Just need to set an additional property on options, don't create a new option variable as below, that didn't help in my case.

Options options = new Options();

Also, setting HTTPConstants.AUTHENTICATE directly doesn't work(as set below)

clientOptions.setProperty(HTTPConstants.AUTHENTICATE, auth);
Babbitt answered 24/11, 2021 at 9:25 Comment(0)
S
0

I found a solution for the problem two months ago. You are not able to set a customized header with Axis2. So I fell back to the older Axisversion, where you can do it. Setting the Http-header by yourself is no good practice and mostly unneccesary. On top it is not a part of the SOAP-specification. That is the reason why you cannot do it with Axis2.

Stines answered 8/1, 2013 at 7:19 Comment(0)
M
0

I too had the same problem the solution is that of Barbiturica: add header option without

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

this page is missleading: reference

Massarelli answered 15/1, 2014 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.