其他插件:设置cookie不起作用

问题描述:

我想在SSO身份验证的服务器上调用经过身份验证的URL。为此,我正在处理请求HTTPClient的Cookie。下面的代码工作正常。其他插件:设置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,它不起作用。见下面的代码片段:

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的问题?它不会产生任何异常或任何警告信息。

+0

http://blog.swwomm.com/2011/01/groovy-httpbuilder-cookies.html – moskiteau 2012-07-05 14:59:42

+0

@stesteau此链接不回答问题。我在编写代码时查看了这个链接,但在上述问题中没有任何参考。 – 2012-07-05 15:45:21

在你的第一个代码片段中,你正在设置Cookie,但是头部实际上是Set-Cookie。