2016-08-01 95 views
0

我有大量从数据库中选取的xml字符串,我想将它们中的每一个都包装到SOAP消息中并发送给收件人。我想用一个常规脚本来做到这一点,就像我用卷曲一样。这意味着我想避免使用wsdl,而是将现有的xml字符串正文封装到soap信封中,然后将其发送到收件人的地址和端口。有没有办法用wslite还是其他的常规SOAP API?Groovy:从信封中创建和发送SOAP请求,而不使用wsdl(如curl)。

回答

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" 
+0

嗯是相同的,如果我的XML代码,而不是JSON身体? – katolsster

+1

对于客户端部分它并不重要 – injecteer

+0

谢谢!有效 :) – katolsster