HttpWebRequest.IfModifiedSince Właściwość

Definicja

Pobiera lub ustawia wartość nagłówka HTTP If-Modified-od .Gets or sets the value of the If-Modified-Since HTTP header.

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

Wartość właściwości

DateTime, który zawiera zawartość nagłówka If-Modified-Since HTTP.A DateTime that contains the contents of the If-Modified-Since HTTP header.

Przykłady

Poniższy przykład kodu sprawdza Właściwość IfModifiedSince.The following code example checks the IfModifiedSince property.

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

Uwagi

Jeśli If-Modified-Since nagłówku jest null, wartość zwracana zostanie ustawiona na DateTime.MinValue.If the If-Modified-Since header is null, then the return value will be set to DateTime.MinValue.

Właściwość IfModifiedSince jest obiektem System.DateTime standardowego i może zawierać System.DateTimeKind pole DateTimeKind.Local, DateTimeKind.Utclub DateTimeKind.Unspecified.The IfModifiedSince property is a standard System.DateTime object and can contain a System.DateTimeKind field of DateTimeKind.Local, DateTimeKind.Utc, or DateTimeKind.Unspecified. Dowolny rodzaj czasu można ustawić przy użyciu właściwości IfModifiedSince.Any kind of time can be set when using the IfModifiedSince property. Jeśli DateTimeKind.Unspecified jest ustawiona lub pobierana, przyjmuje się, że właściwość IfModifiedSince zostanie DateTimeKind.Local (czas lokalny).If DateTimeKind.Unspecified is set or retrieved, the IfModifiedSince property is assumed to be DateTimeKind.Local (local time).

Klasy w przestrzeni nazw System.Net zawsze zapisują Właściwość IfModifiedSince w sieci podczas transmisji w standardowym formacie przy użyciu formatu GMT (UTC).The classes in the System.Net namespace always write it out the IfModifiedSince property on the wire during transmission in standard form using GMT (Utc) format.

Jeśli właściwość IfModifiedSince jest ustawiona na DateTime.MinValue, nagłówek If-Modified-Since HTTP zostanie usunięty z właściwości Headers i WebHeaderCollection.If the IfModifiedSince property is set to DateTime.MinValue, then the If-Modified-Since HTTP header is removed from the Headers property and the WebHeaderCollection.

Jeśli właściwość IfModifiedSince jest DateTime.MinValue, oznacza to, że nagłówek If-Modified-Since HTTP nie jest uwzględniony we właściwości Headers i WebHeaderCollection.If the IfModifiedSince property is DateTime.MinValue, this indicates that the If-Modified-Since HTTP header is not included in the Headers property and the WebHeaderCollection.

Uwaga

Wartość tej właściwości jest przechowywana w WebHeaderCollection.The value for this property is stored in WebHeaderCollection. Jeśli jest ustawiona właściwość WebHeaderCollection, wartość właściwości zostanie utracona.If WebHeaderCollection is set, the property value is lost.

Jeśli jest ustawiona właściwość IfModifiedSince i 304 (nie zmodyfikowano) kod stanu jest zwracany, System.Net.WebException zostanie wygenerowany przez metody BeginGetResponse, GetResponsei EndGetResponse.If the IfModifiedSince property is set and 304 (Not Modified) status code is returned, then a System.Net.WebException will be thrown by the BeginGetResponse, GetResponse, and EndGetResponse methods.

Dotyczy