user2434211
user2434211

Reputation: 21

Send SOAP with Groovy or Java

I've red and try the multiples SOAP messages. I've tried to paste them in my solution but no way of getting the answer.

I use this Groovy code to send SOAP request and get SOAP answer. And it works for 2 web services. I used SoapUI to write my XML and the XML I use here is working on SoapUI, I get the answer from the server.

Now my working XML and my working Groovy script (for other services) are not working together and I don't know how is the problem. I'm not a dev. I've got a SSL error but I'm sure there is no SSL certificate on this server and with SoapUI without SSL it's working and the provider told me there is no certificate to have.

Can you help me and see where is the problem ? Thanks a lot in advance.
Kind regards. Antoine

The Groovy script :

// Send data
URL url = new URL(url);
HttpURLConnection conn = url.openConnection();
conn.setDoOutput(true);
if( soapaction != null ) conn.setRequestProperty( "SOAPAction", soapaction );
conn.setRequestProperty( "Content-Type", "text/xml" );
String authorizationString = "Basic " + (username + ":" +  password).bytes.encodeBase64();
conn.setRequestProperty ("Authorization", authorizationString);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(xml);
wr.close();

// Get the response
String response;
InputStream responseStream;
try {
    responseStream = conn.getInputStream();
    success = 1;
} catch( IOException e ) {
    success = 0;
    if( conn.getResponseCode() == 500 ) {
        responseStream = conn.getErrorStream();
    } else throw e;
}
response = responseStream.getText("utf-8");
responseStream.close();
return response;

Some parameter for this script:
XML
soapaction : getAnimals
URL :https://test.anis.ch/HTDB.WebService/AnimalImportService.asmx
password : test
username : 613731

The XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlso.../soap/envelope/" xmlns:urn="urn:tvd:heimtierdatenbanksql:webservice:animalimportmessages:v1" xmlns:urn1="urn:tvd:heimtierdatenbanksql:webservice:animalimportdata:v1">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:getAnimals>
         <urn:getMessage>
            <urn1:Header>
               <urn1:P_Praxisnummer>371066</urn1:P_Praxisnummer>
               <urn1:P_Account>613731</urn1:P_Account>
               <urn1:P_PIN>test</urn1:P_PIN>
            </urn1:Header>
            <urn1:Body>
               <!--1 or more repetitions:-->
               <urn1:KZ_Kennzeichnung>756000100230345</urn1:KZ_Kennzeichnung>
            </urn1:Body>
         </urn:getMessage>
      </urn:getAnimals>
   </soapenv:Body>
</soapenv:Envelope>

Upvotes: 2

Views: 17556

Answers (2)

Nektototam
Nektototam

Reputation: 11

You have to add next line into script:

conn.setRequestProperty( "Content-Type", "charset=utf-8"); 

Upvotes: 0

tim_yates
tim_yates

Reputation: 171114

You can use groovy wslite to do the same thing (but working) in a lot less code:

@Grab( 'com.github.groovy-wslite:groovy-wslite:0.8.0' )
import wslite.soap.*
import wslite.http.auth.*

def client = new SOAPClient( 'https://test.anis.ch/HTDB.WebService/AnimalImportService.asmx' )

client.authorization = new HTTPBasicAuthorization( "613731", "test" )

// Trust the ssl for this site
client.httpClient.sslTrustAllCerts = true

def response = client.send(SOAPAction:'urn:tvd:heimtierdatenbanksql:webservice:animalimportservcie:v1:getAnimalsIn') {
    body {
        getAnimals( 'xmlns':'urn:tvd:heimtierdatenbanksql:webservice:animalimportmessages:v1' ) {
            getMessage {
                Header( 'xmlns':'urn:tvd:heimtierdatenbanksql:webservice:animalimportdata:v1' ) {
                    P_Praxisnummer( '371066' )
                    P_Account( '613731' )
                    P_PIN( 'test' )
                }
                Body( 'xmlns':'urn:tvd:heimtierdatenbanksql:webservice:animalimportdata:v1' ) {
                    KZ_Kennzeichnung( '756000100230345' )
                }
            }
        }
    }
}

println XmlUtil.serialize( response.getAnimalResponse )

Fingers crossed that works for you!

I get:

<tag0:getAnimalResponse xmlns:tag0="urn:tvd:heimtierdatenbanksql:webservice:animalimportmessages:v1">
  <tag0:outputMessage>
    <R_Fehlertext xmlns="urn:tvd:heimtierdatenbanksql:webservice:animalimportdata:v1">Account-Informationen nicht plausibel</R_Fehlertext>
    <R_FehlerCode xmlns="urn:tvd:heimtierdatenbanksql:webservice:animalimportdata:v1">109</R_FehlerCode>
  </tag0:outputMessage>
</tag0:getAnimalResponse>

So I guess there's something wrong with my credentials...

Upvotes: 4

Related Questions