Groovy / Grails通过HTTP发布XML(使用REST插件)
发布时间:2020-12-14 16:25:02 所属栏目:大数据 来源:网络整理
导读:我正在尝试HTTP使用基本身份验证将 XML字符串发布到WebMethods服务器.我试图使用位于HTTP Builder之上的REST插件.我尝试过一些事情,导致0长度响应.使用Firefox海报我使用了完全相同的XML和用户身份验证,WebMethods响应是用一些额外的信息回显请求,所以我在下
我正在尝试HTTP使用基本身份验证将
XML字符串发布到WebMethods服务器.我试图使用位于HTTP Builder之上的REST插件.我尝试过一些事情,导致0长度响应.使用Firefox海报我使用了完全相同的XML和用户身份验证,WebMethods响应是用一些额外的信息回显请求,所以我在下面的代码中做的是错误的.希望有人有一个指针来执行XML的HTTP Post.
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 干杯, 史蒂夫 解决方法
这就是我最终的结果.它是常见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() } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |