Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans Basic Http Auth Jax-WS

how can I access a webservice through a basic http authentification? I am using the netbeans built in webservice client features. But when I try to access the webservice, I get an exception with a 401 auth failed error message.

How can I pass the right username and password?

Thank you!

like image 728
Pan Avatar asked Dec 18 '09 14:12

Pan


2 Answers

You could use BindingProvider or WSBindingProvider class to access a Web Service through a basic http authentification. The code is as follows.

XxxService service = new XxxService();
Xxx port = service.getXxxPort();

Map<String, Object> reqContext = ((BindingProvider)port).getRequestContext();
reqContext.put(BindingProvider.USERNAME_PROPERTY, "username");
reqContext.put(BindingProvider.PASSWORD_PROPERTY, "password");
like image 163
tomute Avatar answered Nov 15 '22 10:11

tomute


You can also provide your own Authenticator. That way it will work even if the WDSL itself is protected by basic HTTP authentication.

@WebServiceRef(wsdlLocation = "https://laka/sito?wsdl")
static XxxService service;

public static void main(String[] args) {

    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "password".toCharArray());
        }
    });

    service = new XxxService();
    Xxx port = service.getXxxPort();

    // invoke webservice and print response
    XxxResponse resp = port.foo();
    System.out.println(resp.toString());

}
like image 36
mikeltxo Avatar answered Nov 15 '22 10:11

mikeltxo