I am using WebClient
to retrieve a website. I decided to set If-Modified-Since
because if the website hasn't changed, I don't want to get it again:
var c = new WebClient();
c.Headers[HttpRequestHeader.IfModifiedSince] = Last_refreshed.ToUniversalTime().ToString("r");
Where Last_refreshed
is a variable in which I store the time I've last seen the website.
But when I run this, I get a WebException
with the text:
The 'If-Modified-Since' header must be modified using the appropriate property or method.
Parameter name: name
Turns out the API docs mention this:
In addition, some other headers are also restricted when using a
WebClient
object. These restricted headers include, but are not limited to the following:
- Accept
- Connection
- Content-Length
- Expect (when the value is set to "100-continue")
- If-Modified-Since
- Range
- Transfer-Encoding
The
HttpWebRequest
class has properties for setting some of the above headers. If it is important for an application to set these headers, then theHttpWebRequest
class should be used instead of theWebRequest
class.
So does this mean there's no way to set them from WebClient
? Why not? What's wrong with specifying If-Modified-Since
in a normal HTTP GET
?
I know I can just use HttpWebRequest
, but I don't want to because it's too much work (have to do a bunch of casting, can't just get the content as a string).
Also, I know Cannot set some HTTP headers when using System.Net.WebRequest is related, but it doesn't actually answer my question.