HttpWebRequest.IfModifiedSince 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 If-Modified-Since
HTTP 标头的值。
public:
property DateTime IfModifiedSince { DateTime get(); void set(DateTime value); };
public DateTime IfModifiedSince { get; set; }
member this.IfModifiedSince : DateTime with get, set
Public Property IfModifiedSince As DateTime
属性值
包含 HTTP If-Modified-Since
标头内容的 DateTime。
示例
下面的代码示例检查 IfModifiedSince 属性。
// Create a new 'Uri' object with the mentioned string.
Uri^ myUri = gcnew Uri( "http://www.contoso.com" );
// Create a new 'HttpWebRequest' object with the above 'Uri' object.
HttpWebRequest^ myHttpWebRequest = dynamic_cast<HttpWebRequest^>(WebRequest::Create( myUri ));
// Create a new 'DateTime' object.
DateTime targetDate = DateTime::Now;
// Set a target date of a week ago
targetDate.AddDays(-7.0);
myHttpWebRequest->IfModifiedSince = targetDate;
try
{
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse^ myHttpWebResponse = dynamic_cast<HttpWebResponse^>(myHttpWebRequest->GetResponse());
Console::WriteLine( "Response headers \n {0}\n", myHttpWebResponse->Headers );
Stream^ streamResponse = myHttpWebResponse->GetResponseStream();
StreamReader^ streamRead = gcnew StreamReader( streamResponse );
array<Char>^readBuff = gcnew array<Char>(256);
int count = streamRead->Read( readBuff, 0, 256 );
Console::WriteLine( "\nThe contents of Html Page are : \n" );
while ( count > 0 )
{
String^ outputData = gcnew String( readBuff,0,count );
Console::Write( outputData );
count = streamRead->Read( readBuff, 0, 256 );
}
streamResponse->Close();
streamRead->Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse->Close();
Console::WriteLine( "\nPress 'Enter' key to continue................." );
Console::Read();
}
catch ( WebException^ e )
{
if (e->Response)
{
if ( ((HttpWebResponse ^)e->Response)->StatusCode == HttpStatusCode::NotModified)
Console::WriteLine("\nThe page has not been modified since {0}", targetDate);
else
Console::WriteLine("\nUnexpected status code = {0}", ((HttpWebResponse ^)e->Response)->StatusCode);
}
else
Console::WriteLine("\nUnexpected Web Exception {0}" + e->Message);
}
// Create a new 'Uri' object with the mentioned string.
Uri myUri =new Uri("http://www.contoso.com");
// Create a new 'HttpWebRequest' object with the above 'Uri' object.
HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create(myUri);
// Create a new 'DateTime' object.
DateTime targetDate = DateTime.Now;
// Set a target date of a week ago
targetDate.AddDays(-7.0);
myHttpWebRequest.IfModifiedSince = targetDate;
try
{
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("Response headers for recently modified page\n{0}\n",myHttpWebResponse.Headers);
Stream streamResponse=myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe contents of Html Page are : \n");
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
Console.Write(outputData);
count = streamRead.Read(readBuff, 0, 256);
}
// Close the Stream object.
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse.Close();
Console.WriteLine("\nPress 'Enter' key to continue.................");
Console.Read();
}
catch(WebException e)
{
if (e.Response != null)
{
if ( ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.NotModified)
Console.WriteLine("\nThe page has not been modified since "+targetDate);
else
Console.WriteLine("\nUnexpected status code = " + ((HttpWebResponse)e.Response).StatusCode);
}
else
{
Console.WriteLine("\nUnexpected Web Exception " + e.Message);
}
}
' Create a new 'Uri' object with the mentioned string.
Dim myUri As New Uri("http://www.contoso.com")
' Create a new 'HttpWebRequest' object with the above 'Uri' object.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(myUri), HttpWebRequest)
' Create a new 'DateTime' object.
Dim targetDate As DateTime = DateTime.Now
targetDate.AddDays(-7.0)
myHttpWebRequest.IfModifiedSince = targetDate
Try
' Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Console.WriteLine("Response headers for recently modified page" + ControlChars.Cr + "{0}" + ControlChars.Cr, myHttpWebResponse.Headers)
Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim readBuff(256) As [Char]
Dim count As Integer = streamRead.Read(readBuff, 0, 256)
Console.WriteLine(ControlChars.Cr + "The contents of Html Page are : " + ControlChars.Cr)
While count > 0
Dim outputData As New [String](readBuff, 0, count)
Console.Write(outputData)
count = streamRead.Read(readBuff, 0, 256)
End While
' Close the Stream object.
streamResponse.Close()
streamRead.Close()
' Release the HttpWebResponse Resource.
myHttpWebResponse.Close()
Console.WriteLine(ControlChars.Cr + "Press 'Enter' key to continue.................")
Console.Read()
Catch e As WebException
If e.Response IsNot Nothing
If CType(e.Response,HttpWebResponse).StatusCode = HttpStatusCode.NotModified
Console.WriteLine((ControlChars.Cr + "The page has not been modified since " + targetDate))
Else
Console.WriteLine(ControlChars.Cr + "Unexpected status code = " + Ctype(e.Response,HttpWebResponse).StatusCode)
End If
Else
Console.WriteLine(ControlChars.Cr + "Unexpected Web Exception " + e.Message)
End If
End Try
注解
If-Modified-Since
如果标头为 null
,则返回值将设置为 DateTime.MinValue。
属性IfModifiedSince是标准System.DateTime对象,可以包含 System.DateTimeKind 、 DateTimeKind.Utc或 DateTimeKind.Unspecified的DateTimeKind.Local字段。 使用 IfModifiedSince 属性时,可以设置任何类型的时间。 如果 DateTimeKind.Unspecified 已设置或检索 , IfModifiedSince 则假定 DateTimeKind.Local 属性 (本地时间) 。
命名空间中的 System.Net 类始终使用 GMT (Utc) 格式以标准格式在传输过程中写出 IfModifiedSince 网络上的属性。
如果 属性 IfModifiedSince 设置为 DateTime.MinValue,则会 If-Modified-Since
从 Headers 属性和 WebHeaderCollection中删除 HTTP 标头。
IfModifiedSince如果 属性为 DateTime.MinValue,则表示 If-Modified-Since
HTTP 标头未包含在 Headers 属性和 中WebHeaderCollection。
注意
此属性的值存储在 WebHeaderCollection 中。 如果设置了 WebHeaderCollection,则该属性值将丢失。
IfModifiedSince如果设置了 属性,并且 304 (未修改) 状态代码返回,则 System.Net.WebException 、 GetResponse和 EndGetResponse 方法将引发 BeginGetResponse。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈