2

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 WSDL with my browser and Netbeans with a login/password that I have revived from the provider.

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 WSDL. The service do connects to HTTPS, but the error tells HTTP.

What is the correct way to add the username/password to access a Web Service ?

Service provider for the Servlet

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();
}

I tried with providing user/password in the SOAP header to, but with the same result

    ...
    Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();        

    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Username", Collections.singletonList("myuser"));
    headers.put("Password", Collections.singletonList("mypassword"));
    req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    ...

I have looked at these post, but they do not solve my problem:

How do I add a SOAP Header using Java JAX-WS

http://www.mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/

Community
  • 1
  • 1
Chris
  • 712
  • 3
  • 16
  • 39

1 Answers1

4

You can try Storing the WSDL Locally in your project.

See an example in Consuming a Web Service with Java 6 and JAX-WS - Wiki - Confluence.

Then you can use the way in the Specifying the Endpoint Address and HTTP Basic Authorization section, like your first code in your question.

I hope this help you.

GregT
  • 1,039
  • 4
  • 14
  • 34
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • That worked, but now I have to manually check if the WSDL change. In case I get runtime error when the schema change. – Chris Jan 09 '13 at 09:52
  • 1
    [Link](http://shrubbery.mynetgear.net/c/display/W/Consuming+a+Web+Service+with+Java+6+and+JAX-WS) in the answer is dead now. – Naman Gala Mar 23 '15 at 05:41
  • Replaced the link with archive.org backup. Note: code snippets will not display, but are still available in the CDATA sections of the html source. – GregT Nov 21 '17 at 12:05