Set-Cookie header does not set cookie in Internet

2019-07-31 05:27发布

问题:

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!

回答1:

I think because of the domain localhost,you can try visit the URL with 127.0.0.1,and then see the result.