This documentation is archived and is not being maintained.

WebResponse.ContentLength Property

When overridden in a descendant class, gets or sets the content length of data being received.

[Visual Basic]
Public Overridable Property ContentLength As Long
public virtual long ContentLength {get; set;}
public: __property virtual __int64 get_ContentLength();
public: __property virtual void set_ContentLength(__int64);
public function get ContentLength() : long;
public function set ContentLength(long);

Property Value

The number of bytes returned from the Internet resource.


Exception Type Condition
NotSupportedException Any attempt is made to get or set the property, when the property is not overridden in a descendant class.


The ContentLength property contains the length, in bytes, of the response from the Internet resource. For request methods that contain header information, the ContentLength does not include the length of the header information.

Note   The WebResponse class is an abstract (MustInherit in Visual Basic) class. The actual behavior of WebResponse instances at run time is determined by the descendant class returned by WebRequest.GetResponse. For more information about default values and exceptions, please see the documentation for the descendant classes, such as HttpWebResponse and FileWebResponse.


[Visual Basic, C#, C++] The following example uses the ContentLength property to obtain the Length of the resource returned.

[Visual Basic] 

' Create a 'WebRequest' with the specified url.     
Dim myWebRequest As WebRequest = WebRequest.Create("")

' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

' The ContentLength and ContentType received as headers in the response object are also exposed as properties.
   ' These provide information about the length and type of the entity body in the response.
Console.WriteLine(ControlChars.Cr + "Content length :{0}, Content Type : {1}", myWebResponse.ContentLength, myWebResponse.ContentType)


                     // Create a 'WebRequest' with the specified url.
            WebRequest myWebRequest = WebRequest.Create(""); 

            // Send the 'WebRequest' and wait for response.
            WebResponse myWebResponse = myWebRequest.GetResponse(); 

            // Display the content length and content type received as headers in the response object.
            Console.WriteLine("\nContent length :{0}, Content Type : {1}", 
            // Release resources of response object.

// Create a 'WebRequest' with the specified url.
WebRequest* myWebRequest = WebRequest::Create(S"");

// Send the 'WebRequest' and wait for response.
WebResponse* myWebResponse = myWebRequest->GetResponse();

// Display the content length and content type received as headers in the response object.
Console::WriteLine(S"\nContent length : {0}, Content Type : {1}", __box(myWebResponse->ContentLength),

// Release resources of response object.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

WebResponse Class | WebResponse Members | System.Net Namespace | HttpWebResponse.ContentLength