【问题标题】:Rest plugin: Setting cookie does not work休息插件:设置cookie不起作用
【发布时间】: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 有什么问题?它既不会产生任何异常,也不会产生任何警告消息。

【问题讨论】:

标签: grails cookies groovy httpclient httpbuilder


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2013-09-18
    • 1970-01-01
    • 2013-07-10
    • 2022-06-13
    • 1970-01-01
    相关资源
    最近更新 更多