gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:20:34 27 4
gpt4 key购买 nike

我正在尝试使用基本身份验证将 XML 字符串通过 HTTP 发送到 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

干杯,

史蒂夫

最佳答案

这是我最终得到的。这是常见的 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()
}

关于http - Groovy/Grails 通过 HTTP 发布 XML(使用 REST 插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4894579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com