2012-07-05 27 views
0

我想在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

回答

0

在你的第一個代碼片段中,你正在設置Cookie,但是頭部實際上是Set-Cookie。