Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP response header, format for "Expires"

I am setting this in C# with this line:

WebOperationContext.Current.OutgoingResponse.Headers.Add(HttpResponseHeader.Expires, DateTime.Now.AddSeconds(10).ToString());

Now I know the format is off on this as it expects the following: Tue, 06 Dec 2011 20:24:15 GMT

Is there a class in .NET implementing IFormatProvider I could leverage here? Or will I need to create my own?

like image 259
dbobrowski Avatar asked Dec 06 '11 20:12

dbobrowski


People also ask

How do you add expires headers in HTML?

Expires Headers are certain lines of code that tell your browser how long it should keep the cached files from your site. You can add Expires Headers by adding a code such as ExpiresByType image/jpg “access plus 1 month” to your site.

What will happen if you set an Expires header to a later date say 1 day )?

When you set an expires header for a resource, such as all jpeg images, the browser will store those resources in its cache. The next time the visitor comes back to the page it will load faster, as the browser will already have those images available.

What is last modified header HTTP?

The Last-Modified response HTTP header contains a date and time when the origin server believes the resource was last modified. It is used as a validator to determine if the resource is the same as the previously stored one. Less accurate than an ETag header, it is a fallback mechanism.

What is the date header in HTTP?

The Date property represents the value of a Date HTTP header on an HTTP response. The Date header is the date and time the message was sent. Javascript and . NET languages do not use the DateTime object directly.


2 Answers

I use DateTime.UtcNow.AddDays(30).ToString("R")

From MSDN:

The "R" or "r" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.RFC1123Pattern property. The pattern reflects a defined standard, and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'". When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture

like image 56
Jonno Avatar answered Oct 21 '22 09:10

Jonno


You can use the Custom Date and Time Format Strings.

Tue, 06 Dec 2011 20:24:15 GMT

generate the above format like so:

DateTime.Now.AddDays(30).ToUniversalTime()
    .ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'");
like image 42
MikeM Avatar answered Oct 21 '22 08:10

MikeM