Reputation: 35
I am trying yo fetch LastModified date of url but it always returns Today(current date). I have checked many URLs but result is same. I tried both winform and web application.
Here is my code. Please help me to fix it.
Uri myUri = new Uri(TextBox1.Text);
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine("\r\nRequest succeeded and the requested information is in the response , Description : {0}", myHttpWebResponse.StatusDescription);
DateTime today = DateTime.Now;
// Uses the LastModified property to compare with today's date.
if (DateTime.Compare(today, myHttpWebResponse.LastModified) == 0)
Console.WriteLine("\nThe requested URI entity was modified today");
else
{
if (DateTime.Compare(today, myHttpWebResponse.LastModified) == 1)
Console.WriteLine("\nThe requested URI was last modified on:{0}", myHttpWebResponse.LastModified);
// Releases the resources of the response.
myHttpWebResponse.Close();
}
Upvotes: 2
Views: 2186
Reputation: 769
Here's a solution for displaying the Last-Modified date/time of the web page for BOTH html and shtml (server-parsed). I'll also include a php-solution. Let's start with html and shtml. For most servers, html is NOT server-parsed, so when the page is sent to you, the transmission has a Last-Modified variable included. But for server-parsed pages, such as shtml, that variable is NOT sent. Instead, the page designer is supposed to use SSI statements to supply the Last-Modified info, which is processed by the server before the page is sent.
Here's how to have a page handled either way, as straight html that supports Javascript, or as SSI supported shtml.
<p>Last modified:
<!--#config timefmt="%m/%d/%Y %T" --><!--#echo var="LAST_MODIFIED" -->
<script type="text/javascript" language="JavaScript">
<!--
if(Last-Modified !== undefined)
{
document.write(document.lastModified)
}
//-->
</script></p>
The "Last modified: " string is output unconditionally. Then, in server-parsed cases, the comment supplies the date/time using #config timefmt AND #echo SSI-statments. But if this isn't server-parsed, it remains just a comment. Meanwhile, for server-parsed, the variable known as Last-Modified is NOT sent, so the Javascript tests if Last-Modified is undefined, and only does something if it IS defined, which it isn't for server-parsed. CAUTION: In the comments, do NOT leave a blank after !--, and DO leave a blank before -->.
Now the flip side: straight html, not server-parsed. The SSI-statements are NOT executed, so the comment remains just a comment. But Last-Modified is defined, so now the Javescript springs into action and does the document.write to supply a document.lastModified value.
You can play around with formatting, but what's shown above is the basic code.
If the test for Last-Modified !== undefined doesn't work, then you can use a longer method:
<p>Last modified:
<!--#config timefmt="%m-%d-%Y %T" --><!--#echo var="LAST_MODIFIED" -->
<script language="JavaScript">
<!--
var mydate=new Date();
var year=mydate.getFullYear();
var month=mydate.getMonth()+1;
var day=mydate.getDate();
var hours=mydate.getHours();
var minutes=mydate.getMinutes();
var now='';
var testnow='';
var testlast=document.lastModified;
if (month<10) month="0"+month;
if (day<10) day="0"+day;
if (hours<10) hours="0"+hours;
if (minutes<10) minutes="0"+minutes;
now=(month + '/' + day + '/' + year + ' ' + hours + ':' + minutes + ':');
testnow=(now.substring(6,10)+now.substring(0,16));
testlast=(testlast.substring(6,10)+testlast.substring(0,16));
if (testlast && (testlast < testnow))
{ document.write(" "+document.lastModified); }
//-->
</script></p>
This method compares truncated versions of current-date and document.lastModified. The format of both quantities must be identical, and the substring functions drop the "seconds". What most browsers do when document.lastModified doesn't exist is substitute current-date.
Now for the php solution:
<p>Last modified:
<?php
date_default_timezone_set('America/Los_Angeles');
echo " " . date ("m/d/y.", filemtime(__FILE__));
?>
</p>
You can adjust the formatting here as well. In fact, you may want to set the timezone for your server's location. Web Search for "php list of supported timezones".
Upvotes: 0
Reputation: 13716
Per this explanation:
If your website is using plain HTML files, the "Last-Modified" is just the time stamp of the HTML file. If you have, however, dynamic pages that fetch data from a database for example, things are a bit more complex. The server does not know how you are generating your data or how the data can be changed from the last time it was loaded.
So, from most web servers these days, the "Last Modified" date is going to be the date that the page was rendered, because A) configuring the server to know whether the data has changed is extra work that many people won't do, and B) often the data has changed.
Upvotes: 2