Groovy:不使用wsdl(如curl)从信封创建和发送SOAP请求。

时间:2016-08-01 07:19:37

标签: curl soap groovy wsdl

我从数据库中获取了大量的xml字符串,我想将其中的每一个都包装成SOAP消息并发送给收件人。我想用一个groovy脚本来做这件事,就像我用curl做的那样。这意味着我想避免使用wsdl,而是将现有的xml字符串体包装成soap信封,然后将其发送到收件人的地址和端口。有没有办法做到这一点,例如wslite或任何其他SOAP api for groovy?

1 个答案:

答案 0 :(得分:2)

您可以使用HttpBuilder

HTTPBuilder http = new HTTPBuilder( 'http://some.com' )
http.request( POST ){
  uri.path = '/somepath'
  requestContentType = URLENC
  body = [ your:json, envelope:here ]
  headers.Accept = 'application/json'

  response.success = { resp, json ->
    println json
  } 
}

或普通UrlConnection

HttpURLConnection connection = new URL( 'http://some.com/somepath' ).openConnection()
connection.requestMethod = 'POST'
connection.doOutput = true
connection.outputStream.withWriter{ it << "{ some:value }" } // here comes your envelop
connection.connect()
String result
connection.content.withReader{ result = new JsonSlurper().parseText( it.readLine() ).someKey }
log.info "got result $result"