HttpWebRequest.ContentLength Propriedade

Definição

Recebe ou define o Content-length cabeçalho HTTP.

public:
 virtual property long ContentLength { long get(); void set(long value); };
public override long ContentLength { get; set; }
member this.ContentLength : int64 with get, set
Public Overrides Property ContentLength As Long

Valor de Propriedade

O número de bytes de dados a enviar para o recurso da Internet. O padrão é -1, o que indica que a propriedade não foi definida e que não há dados de pedido para enviar.

Exceções

O novo ContentLength valor é inferior a 0.

Exemplos

O seguinte exemplo de código define a ContentLength propriedade para o comprimento da cadeia que está a ser colocada.

// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.Method = "POST";
Console.WriteLine ("\nPlease enter the data to be posted to the (http://www.contoso.com/codesnippets/next.asp) Uri :");

// Create a new string object to POST data to the Url.
string inputData = Console.ReadLine ();


string postData = "firstone=" + inputData;
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (postData);

// Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

// Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length;

Stream newStream = myHttpWebRequest.GetRequestStream ();

newStream.Write (byte1, 0, byte1.Length);
Console.WriteLine ("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength);

// Close the Stream object.
newStream.Close ();
' Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.Method = "POST"

Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the (http://www.contoso.com/codesnippets/next.asp) Uri :")
' Create a new string object to POST data to the Url.
Dim inputData As String = Console.ReadLine()
Dim postData As String = "firstone" + ChrW(61) + inputData
Dim encoding As New ASCIIEncoding()
Dim byte1 As Byte() = encoding.GetBytes(postData)
' Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
' Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length
Dim newStream As Stream = myHttpWebRequest.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength)
newStream.Close()

Observações

Atenção

WebRequest, HttpWebRequest, ServicePoint, e WebClient são obsoletos, e não deves usá-los para novos desenvolvimentos. Utilize HttpClient em substituição.

A ContentLength propriedade contém o valor a enviar como Content-length cabeçalho HTTP com o pedido.

Qualquer valor diferente de -1 na ContentLength propriedade indica que o pedido carrega dados e que apenas métodos que carregam dados podem ser definidos na Method propriedade.

Depois de a ContentLength propriedade ser definida para um valor, esse número de bytes deve ser escrito no fluxo de pedidos que é devolvido ao chamar o GetRequestStream método ou ambos os BeginGetRequestStream métodos e EndGetRequestStream .

Note

O valor desta propriedade é armazenado em WebHeaderCollection. Se o WebHeaderCollection for definido, o valor da propriedade é perdido.

Aplica-se a