0

In my Java code I have this:

exchange.getOut().setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
exchange.getOut().setHeader("Pragma", "no-cache");
exchange.getOut().setHeader("Expires", "Mon, 01 Jan 1990 00:00:00 GMT");

But is it correct to set the Expire date back in time? Should it not be like now + 1 hour or something?

How can I get the date format correctly (Mon, 01 Jan 1990 00:00:00 GMT)?

Europa
  • 974
  • 12
  • 40
  • Never give the date as string, use the `Date` datatype. – sameera sy Jun 04 '18 at 06:27
  • 1
    Correction: Use `LocalDateTime` and `Instant` – OneCricketeer Jun 04 '18 at 06:28
  • Why would you want it an hour in the future? All the other headers you're setting suggest you *don't* want it cached. And what do you mean *"correctly"*, that's what you already have. – jonrsharpe Jun 04 '18 at 06:29
  • I'll try the LocalDateTime. But what should I set it to? – Europa Jun 04 '18 at 06:37
  • Possible duplicate of [What happens if the origin web server sets the expires value in response header as a time which is passed relatively long ago?](https://stackoverflow.com/questions/14536401/what-happens-if-the-origin-web-server-sets-the-expires-value-in-response-header) – Joe Jun 04 '18 at 08:21

1 Answers1

0

It is perfectly correct to specify the Expire-Header in the past. As you can see from the other headers of your snippet, the client should not cache the page at all.

The three given headers all mean the same (redundant), but some are used in older HTTP-Specs (e.g. https://stackoverflow.com/a/10314289/1164913, https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires)

If you want to enable caching of the resource, then you're looking for the RFC-850 date format. (https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1 references https://www.rfc-editor.org/rfc/rfc5322#section-3.3, originating from https://www.rfc-editor.org/rfc/rfc850#section-2.1.4)

Community
  • 1
  • 1
bratkartoffel
  • 1,127
  • 1
  • 13
  • 37