2

我正在尝试获取 URL 的 LastModified 日期,但它总是返回今天(当前日期)。我检查了许多 URL,但结果是一样的。我尝试了 winform 和 web 应用程序。

这是我的代码。请帮我修复它。

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

2 回答 2

2

根据这个解释

如果您的网站使用纯 HTML 文件,“Last-Modified”只是 HTML 文件的时间戳。但是,如果您有从数据库中获取数据的动态页面,那么事情就有点复杂了。服务器不知道您是如何生成数据的,也不知道如何从上次加载数据时更改数据。

因此,从现在的大多数 Web 服务器来看,“上次修改”日期将是页面呈现的日期,因为 A)配置服务器以了解数据是否已更改是许多人不会做的额外工作, 和 B) 数据经常发生变化。

于 2012-10-30T20:36:11.563 回答
0

这是用于显示 html 和 shtml(服务器解析)的网页的 Last-Modified 日期/时间的解决方案。我还将包括一个 php 解决方案。让我们从 html 和 shtml 开始。对于大多数服务器,html 不是服务器解析的,因此当页面发送给您时,传输中包含一个 Last-Modified 变量。但是对于服务器解析的页面,例如 shtml,不会发送该变量。相反,页面设计者应该使用 SSI 语句来提供 Last-Modified 信息,该信息在页面发送之前由服务器处理。

以下是如何以支持 Javascript 的直接 html 或支持 SSI 的 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>

“Last modified:”字符串无条件输出。然后,在服务器解析的情况下,注释使用#config timefmt AND #echo SSI-statments 提供日期/时间。但如果这不是服务器解析的,它仍然只是一个评论。同时,对于服务器解析,不会发送名为 Last-Modified 的变量,因此 Javascript 会测试 Last-Modified 是否未定义,并且仅在已定义时才执行某些操作,这不适用于服务器解析。注意:在评论中,不要在 !-- 之后留下空白,并且在 --> 之前不要留下空白。

现在是另一面:直接的 html,而不是服务器解析的。SSI 语句没有被执行,所以评论仍然只是一个评论。但是 Last-Modified 已定义,所以现在 JavaScript 开始行动并执行 document.write 以提供 document.lastModified 值。

您可以尝试格式化,但上面显示的是基本代码。

如果 Last-Modified !== undefined 的测试不起作用,那么您可以使用更长的方法:

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

此方法比较 current-date 和 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 列表”。

于 2014-01-04T17:30:45.227 回答