Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient HttpResponseMessage LastModified date of file

I am using the C# HttpClient to simulate a request to download a CSV file from a server. I need to check the LastModified dateof the file against the LastModified date of the previous file I downloaded to check if the file has changed.

HttpClient gets a HttpResponseMessage when I make the request, but every time I check

response.Headers.Date.Value

I get the current Date/Time that the request was made. I understand that if the page I'm requesting the file from is generated by a DB or by some other dynamic method, the LastModified value will be the time that the request was made.

However, I have tried the same process using the older HttpWebRequest/Response and I found that

response.Headers[HttpResponseHeader.LastModified]

Will return the date that the file was last changed, e.g. the 2 different methods return different dates, HttpWebResponse gives a date of 1/12/2017, and HttpResponseMessage gives a date of 3/30/2017.

How can I get the file change date using HttpWebResponse?

like image 698
TychoBrahe Avatar asked Mar 30 '17 18:03

TychoBrahe


1 Answers

From https://social.msdn.microsoft.com/Forums/windowsapps/en-US/c830971a-e60f-4759-ba2a-42638b0afad0/uwp-lastmodifiedheader-invalid?forum=wpdevelop

"The Last-Modified HTTP header is treated as part of the HTTP response content rather than the HTTP response itself,"

I was able to pull the LastModified date by accessing the Content of the response headers, not the headers themselves

DateTime lastModified = result.Content.Headers.LastModified;
like image 98
TychoBrahe Avatar answered Oct 03 '22 06:10

TychoBrahe