Groovy / Grails通过HTTP发布XML(使用REST插件) [英] Groovy/Grails posting XML over HTTP (using REST plugin)
问题描述
我正在尝试HTTP使用基本身份验证将XML字符串发布到WebMethods服务器。我试图使用位于HTTP Builder之上的REST插件。我已经尝试了一些全部产生0长度响应的东西。使用Firefox海报我已经使用完全相同的XML和用户身份验证,并且WebMethods响应是用一些额外信息回显请求,所以这是我在下面的代码中做的事情,这是错误的。希望有人有指向做HTTP HTTP Post的指针。
string orderText =< item>
< ;项目> 1< / item>
<价格> 136.000000< / price>
< / item>
def response = withHttp(uri:https://someserver.net:4433){
auth.basic'user','pass'
//已经尝试过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调试HTTPBuilder - 响应代码:200;找到处理程序:OrdersService $ _closure1_closure2_closure3_closure4 @ 36293b29
INFO HTTPBuilder - 状态:200
INFO HTTPBuilder - 数据:空
04-02-2011 14:19:39,896 INFO HTTPBuilder - XML:null
INFO HTTPBuilder - 内容类型:application / EDIINT; charset = UTF-8
INFO HTTPBuilder - 内容长度:0
干杯,
Steve
是我结束了。这是标准的普通HTTP客户端使用
对于通过SSL的基本身份验证,您可以简单地使用您的url: 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响应
尝试{
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执行请求+ httpPost.getRequestLine()
响应= httpClient.execute(httpPost)
HttpEntity resEntity = response.getEntity()
log.info response.getStatusLine()
if(resEntity!= null){
log.with {
infoResponse content length:+ resEntity.getContentLength()
if(isDebugEnabled()){
debugResponse Chunked ?:+ resEntity.isChunked()
debug响应编码:+ resEntity.contentEncoding
debug响应内容:+ resEntity.content.text
}
}
}
// EntityUtils.consume(resEntity);
}
finally {
//当不再需要HttpClient实例时,
//关闭连接管理器以确保
//立即释放所有系统资源
httpClient.getConnectionManager()。shutdown()
}
return response.getStatusLine()
}
I am trying HTTP Post an XML string to a WebMethods server using basic auth. I was trying to use the REST plugin which sits on top of HTTP Builder. I've tried a few things all resulting in a 0 length response. Using Firefox poster I have used the exact same XML and user auth and the WebMethods response is to echo back the request with some extra info, so it is something I am doing in the code below that is wrong. Hope someone has a pointer for doing a HTTP Post of XML.
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
}
Logs say:
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
Cheers,
Steve
Here is what I ended up with. It is quite standard use of common HTTP Client
For basic auth over SSL you can simply have your url like: https://user:pass@www.target.com/etc
Grails remember to copy the HTTPClient jar to the lib folder or in my case I installed the REST plugin which includes HTTPClient anyway.
There are good docs on the HTTPClient site: 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()
}
这篇关于Groovy / Grails通过HTTP发布XML(使用REST插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!