Issue
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/
Solution
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.
Answered By - Paul Vargas