I
I
igor262016-09-11 11:16:20
Java
igor26, 2016-09-11 11:16:20

Java SOAP. Authorization. Can't figure out how to do it?

Good afternoon!
I want to connect to the sletat.ru service ( module.sletat.ru/XmlGate.svc?singlewsdl)
I wrote the following code:

URL url = new URL("http://module.sletat.ru/XmlGate.svc?singlewsdl");

        QName qname = new QName("urn:SletatRu:Contracts:Soap11Gate:v1", "Soap11Gate");

        Service service = Service.create(url, qname);
        Soap11Gate soap = service.getPort(Soap11Gate.class);

But when I try to get some data:
System.out.println(soap.getCountries(6));
It throws the following error:
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: No authorization data provided in AuthData header.
at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:125)
at com.sun .xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
at com.sun .xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:135)
at com.sun.proxy.$Proxy25.getCountries(Unknown Source)
at javarush.client.Sletat.main(Sletat.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application. AppMain.main(AppMain.java:144)
There is a separate class AuthData
I wrote the following code for authorization:
AuthData auth = new AuthData();
        auth.setLogin(new JAXBElement<String>(qname, String.class, "******"));
        auth.setPassword(new JAXBElement<String>(qname, String.class, "*******"));

But I can't figure out how to attach it to soap.
Help, I will be very grateful.
Here is the whole code:
URL url = new URL(" module.sletat.ru/XmlGate.svc?singlewsdl ");
QName qname = new QName("urn:SletatRu:Contracts:Soap11Gate:v1", "Soap11Gate");

        Service service = Service.create(url, qname);
        Soap11Gate soap = service.getPort(Soap11Gate.class);

        AuthData auth = new AuthData();
        auth.setLogin(new JAXBElement<String>(qname, String.class, "*******"));
        auth.setPassword(new JAXBElement<String>(qname, String.class, "*********"));

        System.out.println(soap.getCountries(6));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Kosarev, 2016-09-13
@jaxtr

Try doing something like this before client initialization:

// ...
import java.net.Authenticator;
import java.net.PasswordAuthentication;
// ...
        Authenticator myAuth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password.toCharArray());
            }
        };

        Authenticator.setDefault(myAuth);
// ...

A
Alexander Korobov, 2016-09-19
@superbadlam

This article helped me.
chrisdail.com/2008/08/13/http-basic-authentication...
But I am using cxf apache.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question