Groovy / Grails通过HTTP发布XML(使用REST插件)

时间:2011-02-04 04:31:38

标签: http grails post groovy

我正在尝试使用基本身份验证将XML字符串发布到WebMethods服务器。我试图使用位于HTTP Builder之上的REST插件。我尝试过一些事情,导致0长度响应。使用Firefox海报我使用了完全相同的XML和用户身份验证,WebMethods响应是用一些额外的信息回显请求,所以我在下面的代码中做的是错误的。希望有人有一个指针来执行XML的HTTP帖子。

string orderText = "<item>
  <item>1</item>
  <price>136.000000</price>
</item>"


def response = withHttp(uri: "https://someserver.net:4433") {
      auth.basic 'user', 'pass'

          //  have tried body: XmlUtil.serialize(orderText)
      def r = post(path: '/invoke/document', body: orderText, contentType: XML, requestContentType: XML)
        { resp, xml ->
          log.info resp.status
          log.info resp.data
          resp.headers.each {
            log.info "${it.name} : ${it.value}"
          }
        }
     log.info r
     return r   
}

日志说:

04-02-2011 14:19:39,894 DEBUG HTTPBuilder - Response code: 200; found handler:    OrdersService$_closure1_closure2_closure3_closure4@36293b29
04-02-2011 14:19:39,895  INFO HTTPBuilder - Status: 200
04-02-2011 14:19:39,896  INFO HTTPBuilder - Data: null
04-02-2011 14:19:39,896  INFO HTTPBuilder - XML: null
04-02-2011 14:19:39,913  INFO HTTPBuilder - Content-Type : application/EDIINT; charset=UTF-8
04-02-2011 14:19:39,913  INFO HTTPBuilder - Content-Length : 0

干杯,

史蒂夫

3 个答案:

答案 0 :(得分:5)

这是我最终的结果。它是标准使用的常见HTTP客户端

对于SSL的基本身份验证,您只需输入您的网址:https://user:pass@www.target.com/etc

Grails记得将HTTPClient jar复制到lib文件夹,或者在我的情况下,我安装了包含HTTPClient的REST插件。

HTTPClient网站上有很好的文档:http://hc.apache.org/httpcomponents-client-ga/

import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient 
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient

def sendHttps(String httpUrl, String data) {
    HttpClient httpClient = new DefaultHttpClient()
    HttpResponse response
    try {
        HttpPost httpPost = new HttpPost(httpUrl)
        httpPost.setHeader("Content-Type", "text/xml")

        HttpEntity reqEntity = new StringEntity(data, "UTF-8")
        reqEntity.setContentType("text/xml")
        reqEntity.setChunked(true)

        httpPost.setEntity(reqEntity)
        log.info "executing request " + httpPost.getRequestLine()

        response = httpClient.execute(httpPost)
        HttpEntity resEntity = response.getEntity()

        log.info response.getStatusLine()
        if (resEntity != null) {
            log.with {
                info "Response content length: " + resEntity.getContentLength()
                if (isDebugEnabled()) {
                    debug "Response Chunked?: " + resEntity.isChunked()
                    debug "Response Encoding: " + resEntity.contentEncoding
                    debug "Response Content: " + resEntity.content.text
                }
            }
        }
        // EntityUtils.consume(resEntity);
    }
    finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpClient.getConnectionManager().shutdown()
    }
    return response.getStatusLine()
}

答案 1 :(得分:2)

以这种方式尝试:

HTTPBuilder builder = new HTTPBuilder( url )
builder.request( Method.POST ) { 
       // set uriPath, e.g. /rest/resource
       uri.path = uriPath

       requestContentType = ContentType.XML

       // set the xml body, e.g. <xml>...</xml>
       body = bodyXML

       // handle response
       response.success = { HttpResponseDecorator resp, xml ->
           xmlResult = xml
       }
}

答案 2 :(得分:0)

我想没有必要让它完成那么困难,我使用更简单的方法(顺便说一下你不需要额外的插件)。所以考虑下一段代码,当然我省略了认证部分

class YourController{
    static allowedMethods = [operation:['POST','GET']]

    def operation(){
        def xmlRequest = request.reader.text
        println xmlRequest
        //TODO: XML postprocessing, here you might use XmlParser.ParseText(xmlRequest)
    }
}

我知道这可能是脱离上下文的,因为你要求REST插件,但我想分享这个,因为还有另一种选择。

我正在使用grails 2.3.2和Firefox RESTClient来测试web服务。