C# URL的上次修改日期

C# URL的上次修改日期,c#,.net,C#,.net,我正在尝试获取url的最后修改日期,但它总是返回今天(当前日期)。我已经检查了许多网址,但结果是一样的。我尝试了winform和web应用程序 这是我的密码。请帮我修一下 Uri myUri = new Uri(TextBox1.Text); // Creates an HttpWebRequest for the specified URL. HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(

我正在尝试获取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(); 
      }
Per:

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


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

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

下面介绍如何以两种方式处理页面,一种是支持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 timemt和#echo SSI stations提供日期/时间。但如果这不是服务器解析的,它仍然只是一个注释。同时,对于已解析的服务器,称为Last Modified的变量不会被发送,因此Javascript会测试Last Modified是否未定义,并且只在定义了Last Modified的情况下执行某些操作,而对于已解析的服务器,则不执行此操作。注意:在注释中,请勿在!---,并在-->之前留一个空格

另一方面:直接的html,而不是服务器解析。SSI语句不会执行,因此注释仍然只是一条注释。但是Last Modified已定义,因此现在Javescript启动并执行document.write以提供document.lastModified值

您可以随意设置格式,但上面显示的是基本代码

如果上次修改的测试!==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>
上次修改的代码:

此方法比较当前日期和document.lastModified的截断版本。两个量的格式必须相同,并且子字符串函数会删除“秒”。当document.lastModified不存在时,大多数浏览器所做的是替换当前日期

现在来看php解决方案:

<p>Last modified:
<?php
date_default_timezone_set('America/Los_Angeles');
echo " " . date ("m/d/y.", filemtime(__FILE__));
?>
</p>
上次修改的代码:


您也可以在此处调整格式。实际上,您可能需要为服务器的位置设置时区。通过Web搜索“php支持的时区列表”。

您可以使用Fiddler(或您选择的调试代理)亲自查看服务器的“上次修改”标题,以验证此代码是否正常工作。