0

I hope someone can help me. I have been asked to create a client to call a web service, but the developers of the web service are less than helpful and have provided scant information.

I have created the service client in NetBeans and I have added a handler (from examples I have seen) in order to set username and password for Basic Authentication. However, when I run the project I am told that authentication failed, so either I am sending the wrong information or I need to do some more. What I really don't understand is this: the developers of the web service say that the header should look like the following, so could anyone tell me how I achieve that, or is it all done for me?

POST https:// …

Accept-Encoding: gzip,deflate

Content-Type: text/xml;charset=UTF-8

SOAPAction: ””

Authorization: Basic MjAwKKAwNjAwMTpQcjBlZSRza!:uYWw=

Content-Length: 848

Host: …

Connection: Keep-Alive

User-Agent: Apache-HttpClient/4.1.1

thanks in advance,

Richard
  • 1,070
  • 9
  • 22
  • In the netbeans wizard that creates the service, are there options for 'authentication'? – Richard Feb 18 '15 at 12:24
  • Hi Richard, yes it was autogenerated in netbeans from the wsdl, so it uses jax-ws if that helps? – Persianharry Feb 18 '15 at 12:25
  • Yes sorry. I edited my comment... In the options in Netbeans when you generate the client, does it give you options for 'authentication'? Perhaps you need to regenerate the client? – Richard Feb 18 '15 at 12:26
  • No, I don't get that option. I'll try regenerating, but what about the other header info that is asked for? – Persianharry Feb 18 '15 at 12:30
  • Only the "Authorization" header is important there. Try finding the place where you enter the username and password into the wizard in netbeans. Once you've done that try it again. I suspect your next problem will be the SSL certificates. – Richard Feb 18 '15 at 12:39
  • OK, I regenerated, but still the 'Authenticaion' section of the service properties did not appear. Having said that, I will need to set the username & password in code as it could change for different users etc. so I was trying to do it in a handler. The handler seemed to be firing, so if it is only the authentication that I need to worry about then I'll go back to playing with that. SSL certificates don't seem to be a problem. Thanks for your help. – Persianharry Feb 18 '15 at 12:59
  • No problems. I think you need a NetBeans expert really. I edited your post to include the NetBeans tag. I generally use a framework like CXF of Axis for webservice clients, so I'm not sure exactly what Netbeans does under the hood when you generate a Jax-ws client. Usually though, you can do all this by examining what is available in the "port" and "request" objects etc. that are generated for you. – Richard Feb 18 '15 at 13:04
  • Can you show us the handler where you add the auth headers to the message ? Also can you post the error you get when you invoke the service ? – ramp Feb 18 '15 at 13:32

1 Answers1

0

You can use BindingProvider to set username password for basic authentication. For eg.

    Service service = Service.create(url, qname);
    HelloWorld hello = service.getPort(HelloWorld.class);

    /******************* Set UserName and Password ******************************/
    Map<String, Object> req_context = ((BindingProvider)hello).getRequestContext();
    req_context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);

    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Username", Collections.singletonList("yogesh"));
    headers.put("Password", Collections.singletonList("patil"));
    req_context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    /**********************************************************************/

Also check this thread for more help.

Community
  • 1
  • 1
Yogesh Patil
  • 536
  • 5
  • 20