This discussion is archived
3 Replies Latest reply: May 12, 2013 11:40 AM by 1006833 RSS

How to add a Soap header to a Java client

1006833 Newbie
Currently Being Moderated
Hello,

I am new in Web Services.I am struggling to set Header using Although is has been added through handler. But My requirement is to add Header at client side not from server side. Following test code is being used to set at header despite it is not working .

Map<String, Object> req_ctx = ((BindingProvider)c).getRequestContext();
     req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:7001/CalculatorWS/CalculatorProgram?wsdl");
     
     Map<String, List<String>> headers = new HashMap<String, List<String>>();
     headers.put("Username", Collections.singletonList("mkyong"));
     headers.put("Password", Collections.singletonList("password"));
     req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

Please help me to solve this problem.
  • 1. Re: How to add a Soap header to a Java client
    gimbal2 Guru
    Currently Being Moderated
    Well I don't know. But when I don't know, I Google. In this case simply "jax-ws client add header". And guess what: there were results. This one looks promising:

    http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-client-side/

    Assuming its some JAX-WS implementation you're using of course. In future questions I would be more forthcoming with which exact API and implementation of that API you're using. This is Java, there tend to be at least two but often half a dozen or more alternatives to solving the same problem.
  • 2. Re: How to add a Soap header to a Java client
    1006833 Newbie
    Currently Being Moderated
    Many thanks to reply.

    It had been done by handler.Handler is deploy at server side,In contrast we need to set handler from client side. with the help of google i found piece of code, follow as below.But it is not working for me.

    Need help am i missing something in this code Or what do i use different approach ?




    Map<String, Object> req_ctx = ((BindingProvider)c).getRequestContext();
    req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:7001/CalculatorWS/CalculatorProgram?wsdl");

    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Username", Collections.singletonList("mkyong"));
    headers.put("Password", Collections.singletonList("password"));
    req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
  • 3. Re: How to add a Soap header to a Java client
    1006833 Newbie
    Currently Being Moderated
    Still waiting to find reply.

    Thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points