2 Replies Latest reply on Aug 1, 2013 12:00 PM by user10878887

    JAX-WS setting header HTTP basic authentication



      How can I set HTTP header vars in JAX-WS?

      I found a lot of questions but no answer works.

      This is my code:

      Service service = Service.create(url, portName);
      OnlineSIUIDef hello = service.getPort(OnlineSIUIDef.class);
      Map<String, Object> contextws = ((BindingProvider)service).getRequestContext();
      contextws.put(BindingProvider.USERNAME_PROPERTY, SIUI_USERNAME);
      contextws.put(BindingProvider.PASSWORD_PROPERTY, SIUI_PASSWORD);


      All examples I found on the web are based on such a structure.

      Problem is that first is created a new service, then the header vars are changed.

      But this can't work because when the service is created(at line 1) error 401 is already thrown.


      What is the equivalent in JAX-WS of the following lines?

      HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
      con.setRequestProperty(varName, val);


      Can someone help with this problem?

      I'm looking for a solution for over a day and I can't find nothing usefull about this.

      Thank you.

        • 1. Re: JAX-WS setting header HTTP basic authentication


          Seems that the URL you are referring in Service.create() is a basic auth protected URL(wsdlLocation) ie why you are getting 401 error.

          there are different options to resolve this.

          Authenticator.setDefault(new Authenticator() {
           protected PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(

          try to add the above code ,before creating the URL.

          2 Download the WSDL from Endpoint and refer that WSDL locally,and dynamically hook the correct endpoint using

          contextws.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,"ur Endpoint");

          Haven't tried this...

          custom SSLSocketFactory come in picture only if you have protected your webservice using one way/2 way SSL.





          • 2. Re: JAX-WS setting header HTTP basic authentication

            The service is using 2 way SSL.

            I tried to use the JAX with 2 ways handshake and basic authentication but in the end I used the HTTP protocol.

            HTTP implementation in JAVA is very easy to use with 2 way SSL and basic authentication as opposed to JAX(complicated when you need such some customizations).

            In order to test the service with JAX I need a solution for both problems.


            Thank you.