Set-Cookie header does not set cookie in Internet Explorer

前提是你 提交于 2019-12-24 09:37:52

问题


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.



来源:https://stackoverflow.com/questions/44130346/set-cookie-header-does-not-set-cookie-in-internet-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!