Help with WebHeaderCollection

  • Thread starter Thread starter Roy Chastain
  • Start date Start date
R

Roy Chastain

I need to download a file from a web server only if it has been modified since the last time it was downloaded. I am looking at
using WebClient.DownloadFile. I see that I can set http headers using the Headers Property. I see that there is a header of
If-Modified-Since that can be set using the WebHeaderCollection.Add method. What I don't know is

1) - How does this really work.
By that I mean if I set the value in the header and then call the method, will it only download the file if it has been modified,
or do I have to do some other sort of request and look at the modified time in the returned header and make my own decision about
calling the DownloadFile method or not.

2) - What is the correct format to use to set the header? There is an Add(string) and an Add(string,string). Would it be done
with Add("If-Modified-Since:date_time_string"), If so, what is the format of the date and time string?

Thanks
 
Hi Roy,

Welcome to msdn newsgroup.
As for the using the "If-Modified-Since" http header to avoid download
unchanged duplicated files, I think this depends on how the serverside
service your http web request, the downloaded file is expose as a static
file or emited by a dynamic page such as asp, jsp or aspx ?

the "If-Modified-Since" is a standard http header and if we set this header
, those standard implemented webservers such as IIS, apache will handle
this and determine whether the requested document is expired or not.
however, the webserver is responsible for static documents only, such as
image, css, script file...

If your file is expose by an asp.net web page, that page need manually
checking the "If-Modified-Since" header and determine whether to write out
the file stream or just return a 304 status code.

In addition, in .net we can use the HttpWebRequest class to send http web
requests, and this class has the
"If-Modified-Since" as a strong typed property. (in fact, the webclient
class is using the HttpWebrequest internally).

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemnethttpwebrequ
estclasstopic.asp?frame=true

HTH. Thanks,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Roy Chastain wrote:

[...]
1) - How does this really work.
By that I mean if I set the value in the header and then call the
method, will it only download the file if it has been modified, or do
I have to do some other sort of request and look at the modified time
in the returned header and make my own decision about calling the
DownloadFile method or not.

You either get the updated resource or a 304 Not Modified.
2) - What is the correct format to use to set the header? There is
an Add(string) and an Add(string,string). Would it be done with
Add("If-Modified-Since:date_time_string"), If so, what is the format
of the date and time string?

Let's the read the spec...

If-Modified-Since = "If-Modified-Since" ":" HTTP-date

This is documented in all its glory here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3


Cheers,
 
Back
Top