System.Net.HttpWebRequest.ContentLength Property

Gets or sets the Content-length HTTP header.

Syntax

public override long ContentLength { get; set; }

Value

A long value that specifies the number of bytes of data to send to the Internet resource. The default is -1, which indicates that this value has not been set.

Exceptions

TypeReason
InvalidOperationExceptionData has already been written to the request stream.
ArgumentOutOfRangeExceptionA value less than zero is specified for a set operation.

Remarks

The HttpWebRequest.ContentLength property contains the value to send as the Content-length HTTP header with the request.

Any value other than -1 in the HttpWebRequest.ContentLength property indicates that the request uploads data and that only methods that upload data are allowed to be set in the HttpWebRequest.Method property.

After the HttpWebRequest.ContentLength property is set to a value, that number of bytes must be written to the request stream that is returned by calling the HttpWebRequest.GetRequestStream method or both the HttpWebRequest.BeginGetRequestStream(AsyncCallback, object) and the HttpWebRequest.EndGetRequestStream(IAsyncResult) methods.

Note:

The value for this property is stored in System.Net.WebHeaderCollection. If WebHeaderCollection is set, the property value is lost.

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0