Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java web service client, adding http headers

Tags:

Having created a java web service client using wsimport on a wsdl, I need to set the Authorization header for each soap message embedded in an http request. Having generated a subclass of javax.xml.ws.Service, how can I append an http header to each outgoing request???

like image 298
murungu Avatar asked Jul 12 '11 14:07

murungu


People also ask

How do I add HTTP headers?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.


1 Answers

Here is the code, based on Femi's answer.

It can be a little tricky to figure out. Works beautifully!

Service jaxwsService = Service.create(wsdlURL, serviceName); Dispatch<SOAPMessage> disp = jaxwsService.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);  //Add HTTP request Headers Map<String, List<String>> requestHeaders = new HashMap<>(); requestHeaders.put("Auth-User", Arrays.asList("BILL_GATES")); disp.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders); 
like image 136
Daniel Alexiuc Avatar answered Nov 06 '22 05:11

Daniel Alexiuc