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/

1 Answer 1

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.

3
  • 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 9, 2013 at 9:52
  • 1
    Link in the answer is dead now.
    – Naman Gala
    Mar 23, 2015 at 5: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, 2017 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.