简体   繁体   中英

Set-Cookie header does not set cookie in Internet Explorer

I'm trying to set a cookie server-side using the Set-Cookie header.

Using jersey the cookie is set server side like this:

NewCookie cookie  = new NewCookie("token", tokenValue, "/", "", 1, "", 3600, new Date(System.currentTimeMillis() + 3600000), false, false);
return Response.ok()
                .cookie(cookie)
                .build();

My response header in Chrome looks like this:

在此输入图像描述

When I try to send another request to the server, to check if the cookie is send back, everything works as expected. The request header looks like this:

在此输入图像描述

Firefox and Opera browsers also show the same behavior. Although, when I try Internet Explorer, there's another story...

Response headers of the first request: 在此输入图像描述

Headers of the second request: 在此输入图像描述

Basically there are no request headers, and the cookie is not set... Am I doing something wrong, when I set the cookie? I have tried various solutions from other similar questions, but nothing seems to work...

EDIT:

Changed the IE Internet options by disabling protected mode and allowed all cookies, but still nothing...

在此输入图像描述

EDIT 2:

Trying it on different computers, I get mixed results. In some computers it works properly, and in some it doesn't. There must be some settings on the Internet Explorer that I am missing. Although, no matter what I try, I cannot get it to work on localhost ...

SOLUTION

Apparently, as dabaicai commented there should not be any empty attribute-value fileds. when I created my cookie the domain and comment atrribute had empty values:

NewCookie cookie  = new NewCookie("token", tokenValue, "/", "", 1, "", 3600, new Date(System.currentTimeMillis() + 3600000), false, false);

I changed it to:

NewCookie cookie  = new NewCookie("token", tokenValue, "/", httpServletRequest.getServerName(), 1, "no-comment", 3600, new Date(System.currentTimeMillis() + 3600000), false, false);

And now everything works as expected in Internet Explorer too!

我认为由于域localhost ,您可以尝试访问127.0.0.1的URL,然后查看结果。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM