java - 使用 Groovy 或 Java 发送 SOAP

标签 java web-services soap groovy

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

我使用此 Groovy 代码发送 SOAP 请求并获得 SOAP 应答。它适用于 2 个 Web 服务。我使用 SoapUI 编写我的 XML,我在这里使用的 XML 在 SoapUI 上工作,我从服务器得到答案。

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

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

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;

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

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>

最佳答案

您可以使用 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 )

祝你好运!

我得到:

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

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

关于java - 使用 Groovy 或 Java 发送 SOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19941770/

相关文章:

java - 想要计算标记的平均值但它不会打印小数点(java)

wcf - 如何在.net core项目中设置soapclient的超时

delphi - 请求过于频繁时,THTTPRIO、SOAP 和错误 503

c - 在不检查证书的情况下使用 OpenSSL,但我的数据是否仍在加密?

java - API 10 编译错误

java - 使用java代码访问json字符串

java - JAX-WS + hibernate + JAXB : How to avoid LazyInitializationException during marshalling

php - 带有 Silex PHP : good choice? 的 RESTful 网络服务

java - 集合层次结构应该是 Collection (Read Only) -> ModulatedCollection

.net - 使用 .NET 3.5 创建新的 Soap Web 服务