【发布时间】:2012-07-05 10:44:51
【问题描述】:
我想在经过 SSO 验证的服务器上调用经过验证的 URL。为此,我正在处理对 HTTPClient 的请求中的 cookie。下面的代码工作正常。
def cookies = []
request.getCookies().each {
def cookie = new BasicClientCookie(it.name, it.value)
cookie['domain'] = it.domain
cookie['path'] = it.path
cookie.secure = true
cookies.add(cookie)
}
// **** Setting cookies using header *****
def output = withHttp(uri: "https://testserver.com") {
def builder = delegate;
def html = get(path : '/testactoin.do',
headers:['Cookie':cookies.collect{it.name+"="+it.value}.join("; ")],
contentType : ContentType.XML,
query :
[
query: params.query,
count: params.count,
cacheName: 'contentStoreCityState',
filterString: 'address.country=CA,GB,US'
]
)
return html
}
但是,如果我尝试使用 api 设置 cookie,它就不起作用。参见下面的代码 sn-p:
def cookies = []
request.getCookies().each {
def cookie = new BasicClientCookie(it.name, it.value)
cookie['domain'] = it.domain
cookie['path'] = it.path
cookie.secure = true
cookies.add(cookie)
}
def output = withHttp(uri: "https://testserver.com") {
def builder = delegate;
// **** Setting cookies using api call *****
cookies.each {
builder.client.cookieStore.addCookie(it)
}
def html = get(path : '/testactoin.do',
contentType : ContentType.XML,
query :
[
query: params.query,
count: params.count,
cacheName: 'contentStoreCityState',
filterString: 'address.country=CA,GB,US'
]
)
return html
}
使用 addCookie 方法设置 cookie 有什么问题?它既不会产生任何异常,也不会产生任何警告消息。
【问题讨论】:
-
@stesteau 这个链接没有回答这个问题。我在编写代码时查看了这个链接,但它没有任何参考如果出现上述问题该怎么办。
标签: grails cookies groovy httpclient httpbuilder