Export (0) Print
Expand All

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
[C#]
public virtual long ContentLength {get; set;}
[C++]
public: __property virtual __int64 get_ContentLength();
public: __property virtual void set_ContentLength(__int64);
[JScript]
public function get ContentLength() : long;
public function set ContentLength(long);

Property Value

The number of bytes returned from the Internet resource.

Exceptions

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

Remarks

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.

Example

[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("www.contoso.com")

' 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)
myWebResponse.Close()


[C#] 

                     // Create a 'WebRequest' with the specified url.
            WebRequest myWebRequest = WebRequest.Create("http://www.contoso.com"); 

            // 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}", 
                                         myWebResponse.ContentLength, 
                                         myWebResponse.ContentType);  
            
            // Release resources of response object.
            myWebResponse.Close(); 
            

[C++] 
// Create a 'WebRequest' with the specified url.
WebRequest* myWebRequest = WebRequest::Create(S"http://www.contoso.com");

// 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),
   myWebResponse->ContentType);

// Release resources of response object.
myWebResponse->Close();

[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.

Requirements

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

Show:
© 2014 Microsoft