HttpWebRequest.IfModifiedSince does not have a setter

How are we supposed to set the IfModifiedSince header on HttpWebRequest? It seems that it has no setter:

    using (var req = HttpWebRequest.Create(url) as HttpWebRequest)
    {
        req.KeepAlive = false;
        req.HttpsAuthentCerts = certx509;
        req.ReadWriteTimeout = 2000;
        if (timestamp > DateTime.MinValue)
        {
            req.IfModifiedSince = timestamp;  // <-- Compilation error here
        }

The docs at https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.ifmodifiedsince?view=netframework-4.8 show it working with a setter:

HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create(myUri);
// Create a new 'DateTime' object.
DateTime targetDate = DateTime.Now;
// Set a target date of a week ago
targetDate.AddDays(-7.0);
myHttpWebRequest.IfModifiedSince = targetDate;

Bug or by-design?

I’m presuming this is a bug. Filed as https://github.com/ghi-electronics/TinyCLR-Libraries/issues/511

1 Like

Fixed in pull request https://github.com/ghi-electronics/TinyCLR-Libraries/pull/514

1 Like

Hello,

May I ask why you did not use the “standard” pattern ?

private string myDateToRFC1123String(DateTime d)
        {
            return 
  d.ToUniversalTime().ToString(CultureInfo.InvariantCulture.DateTimeFormat.RFC1123Pattern);
        }

That said, both codes do not seem to return the same string :frowning: But I’m not an expert so I don’t know which one is correct. So you may be right :wink:

2 Likes

If that works in TinyCLR, then it’s much neater. I will test and if it works, will update the pull.

Yup - that works and is much cleaner. I didn’t know we had that format available so hat tip to @Bec_a_Fuel - thanks!

The PR has been updated.

1 Like

You’re welcome.