使用 Groovy 或 Java 发送 SOAP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19941770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:43:11  来源:igfitidea点击:

Send SOAP with Groovy or Java

javaweb-servicessoapgroovy

提问by user2434211

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.

我已经红色并尝试了多个 SOAP 消息。我试图将它们粘贴到我的解决方案中,但无法得到答案。

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.

我使用这个 Groovy 代码发送 SOAP 请求并获得 SOAP 应答。它适用于 2 个网络服务。我使用 SoapUI 来编写我的 XML,我在这里使用的 XML 正在处理 SoapUI,我从服务器得到了答案。

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.

现在我的工作 XML 和我工作的 Groovy 脚本(用于其他服务)不能一起工作,我不知道问题出在哪里。我不是开发人员。我遇到了 SSL 错误,但我确定此服务器上没有 SSL 证书,并且没有 SSL 的 SoapUI 可以正常工作,但提供商告诉我没有证书。

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

你能帮我看看问题出在哪里吗?非常感谢。
亲切的问候。安托万

The Groovy script :

Groovy 脚本:

// 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

此脚本的一些参数:
XML
soapaction : getAnimals
URL : https://test.anis.ch/HTDB.WebService/AnimalImportService.asmx
密码 : 测试
用户名 : 613731

The XML:

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>

回答by tim_yates

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

您可以使用groovy wslite以更少的代码做同样的事情(但工作):

@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...

所以我想我的凭据有问题......

回答by Nektototam

You have to add next line into script:

您必须将下一行添加到脚本中:

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