使用HTTPBuilder发送POST请求而不等待响应
我有以下代码:使用HTTPBuilder发送POST请求而不等待响应,post,groovy,httprequest,Post,Groovy,Httprequest,我有以下代码: ApiConsumer(String url) { this.baseUrl = url this.httpBuilder = initializeHttpBuilder() this.cookies = [] } private HTTPBuilder initializeHttpBuilder() { def httpBuilder = new HTTPBuilder(baseUrl) httpBuilder.handler.succ
ApiConsumer(String url) {
this.baseUrl = url
this.httpBuilder = initializeHttpBuilder()
this.cookies = []
}
private HTTPBuilder initializeHttpBuilder() {
def httpBuilder = new HTTPBuilder(baseUrl)
httpBuilder.handler.success = { HttpResponseDecorator resp, reader ->
resp.getHeaders('Set-Cookie').each {
String cookie = it.value.split(';')[0]
cookies.add(cookie)
}
return convertPlain("${reader}")
}
return httpBuilder
}
public def requestXML(Method method, ContentType contentType, String url, String bodyXML) {
httpBuilder.parser.'application/xml' = httpBuilder.parser.'text/plain'
httpBuilder.request(method, contentType) { request ->
uri.path = url
body = bodyXML
headers['Cookie'] = cookies.join(';')
}
}
基本上,使用requestXML(…)
它使用httpbuilderforgroovy向指定的URL发送XML请求。
我正在使用这段代码(以及其他一些次要功能)向服务发送请求,它可以正常工作。
但现在我想重用它,向另一个服务发出POST请求,该服务将在30分钟后响应,因为该WPS服务运行一个程序并等待程序结束。如何在不等待回复的情况下发送此POST请求
我需要设置一个超时?
我试图删除httpBuilder.handler.success
闭包,但没有成功。
此外,我无法更改WPS服务处理请求的方式。请尝试使用
AsyncHttpBulder
,如下所述:
例如:
import groovyx.net.http.AsyncHTTPBuilder
import static groovyx.net.http.ContentType.HTML
def http = new AsyncHTTPBuilder(
poolSize : 4,
uri : 'http://hc.apache.org',
contentType : HTML )
def result = http.get(path:'/') { resp, html ->
println ' got async response!'
return html
}
assert result instanceof java.util.concurrent.Future
while ( ! result.done ) {
println 'waiting...'
Thread.sleep(2000)
}
/* The Future instance contains whatever is returned from the response
closure above; in this case the parsed HTML data: */
def html = result.get()
assert html instanceof groovy.util.slurpersupport.GPathResult