2012-10-30 41 views
2

我想喲取URL的LastModified日期,但它總是返回今天(當前日期)。我檢查了很多網址,但結果相同。我嘗試了winform和web應用程序。URL的LastModified日期

這是我的代碼。請幫我解決它。

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(); 
     } 
+0

http://stackoverflow.com/questions/6338839/how-to-get-last-modified-date-of-the-webpage-in-c –

+0

您可以通過使用Fiddler來驗證此代碼是否正常工作(或您選擇的調試代理)爲自己查看服務器的「上次修改」標題。 – David

回答

2

this explanation

如果你的網站是使用純HTML文件中,「上次修改」只是時間的HTML文件的時間戳。但是,如果您擁有從數據庫獲取數據的動態頁面,則情況會更復雜一些。服務器不知道您是如何生成數據的,也不知道如何從上次加載數據時更改數據。

所以,從現在的大多數Web服務器來看,「上次修改」日期將是頁面呈現的日期,因爲A)配置服務器以瞭解數據是否已更改是額外的工作,很多人不會這樣做,而且B)經常將數據改爲

0

下面是顯示BOTH html和shtml(server-parsed)網頁的Last-Modified日期/時間的解決方案。我還將包含一個php解決方案。我們從html和shtml開始。對於大多數服務器,html不是服務器解析的,所以當頁面發送給你時,傳輸包含一個Last-Modified變量。但對於服務器分析的頁面(如shtml),該變量不會發送。相反,頁面設計者應該使用SSI語句來提供Last-Modified信息,這是在發送頁面之前由服務器處理的。

下面介紹如何以任何方式處理頁面,如支持Javascript的直線html或支持shtml的SSI。

<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> 

無條件輸出「Last modified:」字符串。然後,在服務器分析的情況下,註釋使用#config timefmt和#echo SSI-statments提供日期/時間。但是,如果這不是服務器分析,它仍然只是一個評論。同時,對於服務器分析,不會發送稱爲Last-Modified的變量,因此Javascript會測試Last-Modified是否未定義,並且只在定義了它時才執行某些操作,而不是針對服務器分析的。小心:在註釋中,請勿在! - 之後留下空白,並且請在 - 之前留空。

現在的另一面:直的HTML,而不是服務器分析。 SSI語句不會被執行,所以評論仍然只是一個評論。但是Last-Modified被定義了,所以現在Javescript開始行動並執行document.write來提供document.lastModified值。

你可以玩格式化,但上面顯示的是基本代碼。

如果測試的Last-Modified ==未定義不工作,那麼你可以使用更長的方法:

<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> 

這種方法比較當前最新和document.lastModified截短形式。兩個數量的格式必須相同,並且子字符串函數會減少「秒數」。當document.lastModified不存在時,大多數瀏覽器執行的操作是替代當前日期。

現在的PHP解決方案:

<p>Last modified: 
<?php 
date_default_timezone_set('America/Los_Angeles'); 
echo " " . date ("m/d/y.", filemtime(__FILE__)); 
?> 
</p> 

你可以在這裏調整格式爲好。實際上,您可能需要爲服務器的位置設置時區。網頁搜索「支持時區的php列表」。