HttpListenerResponse.ContentLength64 Propiedad

Definición

Obtiene o establece el número de bytes de los datos del cuerpo incluidos en la respuesta.

C#
public long ContentLength64 { get; set; }

Valor de propiedad

Valor del encabezado Content-Length de la respuesta.

Excepciones

El valor especificado para una operación de conjunto es menor que cero.

Ya se está enviando la respuesta.

Este objeto está cerrado.

Ejemplos

En el ejemplo de código siguiente se muestra cómo establecer el valor de esta propiedad.

C#
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
    if (!HttpListener.IsSupported)
    {
        Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
        return;
    }
    // URI prefixes are required,
    // for example "http://contoso.com:8080/index/".
    if (prefixes == null || prefixes.Length == 0)
      throw new ArgumentException("prefixes");

    // Create a listener.
    HttpListener listener = new HttpListener();
    // Add the prefixes.
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request.
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Construct a response.
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer,0,buffer.Length);
    // You must close the output stream.
    output.Close();
    listener.Stop();
}

Comentarios

El Content-Length encabezado expresa la longitud, en bytes, de los datos del cuerpo de la respuesta. Al usar un formato que no envía los datos fragmentados o sin procesar, debe establecer la ContentLength64 propiedad . Si no lo hace, no HttpListener envía los datos de respuesta.

Para obtener una lista completa de encabezados de respuesta, consulte la HttpResponseHeader enumeración .

Se aplica a

Producto Versiones
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Consulte también