This documentation is archived and is not being maintained.

WebRequest.ContentLength Property

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

[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 of request data being sent.

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 number of bytes of data sent to the Internet resource by the WebRequest instance.

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

Example

[Visual Basic, C#, C++] The following example sets the ContentLength property to the amount of bytes in the outgoing byte buffer.

[Visual Basic] 


' Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType = "application/x-www-form-urlencoded"

' Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength = byteArray.Length
Dim newStream As Stream = myWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)

' Close the Stream object.
newStream.Close()

' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()


[C#] 

            // Set the 'ContentType' property of the WebRequest.
            myWebRequest.ContentType="application/x-www-form-urlencoded";

            // Set the 'ContentLength' property of the WebRequest.
            myWebRequest.ContentLength=byteArray.Length;
            Stream newStream=myWebRequest.GetRequestStream();
            newStream.Write(byteArray,0,byteArray.Length);

            // Close the Stream object.
            newStream.Close();

            // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
            WebResponse myWebResponse=myWebRequest.GetResponse();
            

[C++] 
// Set the 'ContentType' property of the WebRequest.
myWebRequest->ContentType=S"application/x-www-form-urlencoded";

// Set the 'ContentLength' property of the WebRequest.
myWebRequest->ContentLength=byteArray->Length;
Stream* newStream=myWebRequest->GetRequestStream();
newStream->Write(byteArray, 0, byteArray->Length);

// Close the Stream Object*.
newStream->Close();

// Assign the response Object* of 'WebRequest' to a 'WebResponse' variable.
WebResponse* myWebResponse=myWebRequest->GetResponse();

[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

WebRequest Class | WebRequest Members | System.Net Namespace | HttpWebRequest.ContentLength

Show: