【问题标题】:Set-Cookie Header is in the response but browser doesn't set Cookie in next requestSet-Cookie 标头在响应中,但浏览器未在下一个请求中设置 Cookie
【发布时间】:2012-05-02 09:14:47
【问题描述】:

我正在开发一个需要有状态的 REST Api(没有解决方法,我使用的是没有可序列化对象等的本机库)

此 Rest API 在域 domainA 上的 Web 服务器上公开。 响应包含一个设置为 * 的 Access-Control-Allow-Origin 标头,所以我不需要 JSON-P。 (我不知道这个细节是否在意)。

我在请求 DomainA 的 domainB 上做了这个 API 的 GWT 使用示例。浏览器不会添加之前设置的 cookie。它“无状态”地工作。

样本也必须在 DomainA 上吗? 是否有解决方法或特定标题要设置?

编辑: 我在 Cookie 中设置了一个主域。它不起作用。浏览器仍然没有在下一个请求头中设置 cookie 会话。

请求:

HeadersPreviewResponseCookiesTiming
Request URL:http://subdomainB.mydomain.com/request
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:1185
Content-type:application/x-www-form-urlencoded
Host:subdomainB.mydomain.com
Origin:http://subdomainA.mydomain.com
Referer:http://subdomainA.mydomain.com/BLABLABLA
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.8 (KHTML, like Gecko) Chrome/20.0.1105.0 Safari/536.8

回复:

Response Headersview source
Access-Control-Allow-Origin:*
Cache-Control:no-cache, no-store, max-age=0
Connection:Keep-Alive
Content-Language:fr-FR
Content-Type:application/json;charset=UTF-8
Date:Wed, 25 Apr 2012 07:59:03 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT, Thu, 01 Jan 1970 00:00:00 GMT
Keep-Alive:timeout=15, max=100
Pragma:no-cache
Server:Jetty(7.5.4.v20111024)
Set-Cookie:JSESSIONID=cookieValue;Path=/;Domain=.mydomain.com
Transfer-Encoding:chunked

【问题讨论】:

  • 对此的任何更新。你是怎么解决这个问题的

标签: json http session rest cookies


【解决方案1】:

根据standards spec,正确的语法是:

set-cookie-header = "Set-Cookie:" SP set-cookie-string
set-cookie-string = cookie-pair *( ";" SP cookie-av )
cookie 对 = cookie 名称 "=" cookie 值
...

所以您似乎缺少的是每个分号后的空格字符。

【讨论】:

    猜你喜欢
    • 2014-08-24
    • 2016-06-06
    • 2019-04-26
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2019-01-26
    • 2023-03-25
    相关资源
    最近更新 更多