0

我想在经过 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,它就不起作用。请参阅下面的代码片段:

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 有什么问题?它既不会产生任何异常,也不会产生任何警告消息。

4

1 回答 1

0

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

于 2013-08-08T14:17:01.640 回答