title: How to authenticate a Web Service Client with JAX-WS / SOAP
body: <p>I am new to web service and tried some tutorials. But I stumble on patrol when I try to access a HTTPS secure SOAP web service. I can access the <code>WSDL</code> with my browser and Netbeans with a login/password that I have revived from the provider.</p>
  
  <p>Netbeans generate service/schema classes (via authentication) and I run my client on Glassfish 3.1.2. I have added a Servlet to access the Service. But I get a HTTP response code: 401 (=Unauthorized) when trying to access the service and <code>WSDL</code>. The service do connects to <code>HTTPS</code>, but the error tells <code>HTTP</code>.</p>
  
  <p>What is the correct way to add the username/password to access a Web Service ?</p>
  
  <p>Service provider for the Servlet</p>
  
  <pre><code>private static LogicalModel pullpullEnterpriseService() {
      pullEnterpriseService service = new pullEnterpriseService();        
      ClientPullSoapBinding port = service.getClientPullSoapBinding();        
  
      BindingProvider prov = ((BindingProvider)port);
      prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "myuser");
      prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "mypassword");
  
      return port.getDataModel();
  }
  </code></pre>
  
  <p>I tried with providing user/password in the SOAP header to, but with the same result        </p>
  
  <pre><code>    ...
      Map&lt;String, Object&gt; req_ctx = ((BindingProvider)port).getRequestContext();        
  
      Map&lt;String, List&lt;String&gt;&gt; headers = new HashMap&lt;String, List&lt;String&gt;&gt;();
      headers.put("Username", Collections.singletonList("myuser"));
      headers.put("Password", Collections.singletonList("mypassword"));
      req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
      ...
  </code></pre>
  
  <p>I have looked at these post, but they do not solve my problem:</p>
  
  <p><a href="https://stackoverflow.com/questions/830691/how-do-i-add-a-soap-header-using-java-jax-ws?rq=1">How do I add a SOAP Header using Java JAX-WS</a></p>
  
  <p><a href="http://www.mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/" rel="nofollow noreferrer">http://www.mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/</a></p>