[英]Send SOAP with Groovy or Java
我已經紅了並嘗試了多次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
URL: 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>
所以我猜我的證書有問題......
您必須在腳本中添加下一行:
conn.setRequestProperty( "Content-Type", "charset=utf-8");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.