HttpWebRequest.IfModifiedSince Propiedad

Definición

Obtiene o establece el valor del encabezado HTTP If-Modified-Since.

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

Valor de propiedad

DateTime que contiene el contenido del encabezado HTTP If-Modified-Since.

Ejemplos

En el ejemplo de código siguiente se comprueba la IfModifiedSince propiedad .

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

Comentarios

Si el If-Modified-Since encabezado es null, el valor devuelto se establecerá en DateTime.MinValue.

La IfModifiedSince propiedad es un objeto estándar System.DateTime y puede contener un System.DateTimeKind campo de DateTimeKind.Local, DateTimeKind.Utco DateTimeKind.Unspecified. Cualquier tipo de tiempo se puede establecer cuando se usa la IfModifiedSince propiedad . Si DateTimeKind.Unspecified se establece o recupera, se supone que la IfModifiedSince propiedad es DateTimeKind.Local (hora local).

Las clases del System.Net espacio de nombres siempre lo escriben IfModifiedSince en la propiedad en la conexión durante la transmisión en formato estándar mediante el formato GMT (Utc).

Si la IfModifiedSince propiedad se establece DateTime.MinValueen , el If-Modified-Since encabezado HTTP se quita de la Headers propiedad y .WebHeaderCollection

Si la IfModifiedSince propiedad es DateTime.MinValue, indica que el If-Modified-Since encabezado HTTP no se incluye en la Headers propiedad y .WebHeaderCollection

Nota

El valor de esta propiedad se almacena en WebHeaderCollection. Si se establece WebHeaderCollection, se pierde el valor de propiedad.

Si se establece la IfModifiedSince propiedad y se devuelve el código de estado 304 (sin modificar), los métodos , GetResponsey EndGetResponse producirán BeginGetResponseuna System.Net.WebException excepción .

Se aplica a