3

Im trying to set a cookie with the value /sv/en and the / ends up like %2F in the cookie, so I wonder how to encode or whatever you have to do to write a / instead.

Im using this to set the cookie now.

 Response.Cookies("googtrans")="/sv/en"
 Response.Cookies("googtrans").path = "/"
 Response.Cookies("googtrans").expires = "2038-01-01 10:00"
 Response.Cookies("googtrans").domain = ".mypage.se"

So how can I write a / in the cookie? Thanks a lot!

Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86

2 Answers2

2

Through the Response.Cookies collection URL Encoding (and vice versa URL Decoding) is implicit.

To prevent this, you need to set a raw cookie with Response.AddHeader instead.

Response.AddHeader "Set-Cookie", "googtrans=/sv/en; expires=2038-01-01 10:00; domain=.mypage.se; path=/"
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • Thanks, Kul-Tigin. It works! But how do I set if I want the domain to be www.mypage.se as well as .mypage.se? If I use Response.AddHeader "Set-Cookie", "googtrans=/sv/en; expires=2038-01-01 10:00; domain=www.mypage.se; path=/" then it becomes .www.mypage.se and not www.mypage.se - see the dot before www. Thanks. – Claes Gustavsson Mar 07 '17 at 12:52
  • I simply want to set that the google cookie "googtrans" never expires, with add header or javascript somehow, and this is almost right :-) – Claes Gustavsson Mar 07 '17 at 12:54
  • @ClaesGustavsson `.mypage.se` will work all the subdomains of `mypage.se` including domain name itself too. – Kul-Tigin Mar 07 '17 at 12:56
  • Ok, but when I use google translate on my page it set 2 cookies, one with mypage.se and one with www.mypage.se and both have expires set to session, if I change just mypage.se to have an expiration date, then it is not working, I need to be able to change both cookies, so both cookies gets the same expiration date. – Claes Gustavsson Mar 07 '17 at 13:18
  • @ClaesGustavsson then set two different (`Response.AddHeader`) raw cookies as shown for `mypage.se` and `www.mypage.se` by removing the parts `expires=2038-01-01 10:00; ` – Kul-Tigin Mar 07 '17 at 13:33
  • OK, if I set this: Response.AddHeader "Set-Cookie", "googtrans=/sv/en; expires=Thu, 18 Dec 2038 12:00:00; domain=www.mypage.se; path=/" then the cookie domain becomes ".www.mypage.se" and not "www.mypage.se", it adds a dot before the "www" and thats is the problem. – Claes Gustavsson Mar 07 '17 at 13:45
0

You cannot change this behavior with ASP because it is a function of the browser.

Cookies get stored/transmitted that way by the browser because the cookies are communicated over HTTP in the headers. The slashes are encoded according to the URLEncoding schema.

Robert S
  • 496
  • 4
  • 14