WebRequest.ContentLength 屬性

定義

在子代類別中覆寫時,取得或設定正在傳送要求資料的內容長度。When overridden in a descendant class, gets or sets the content length of the request data being sent.

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

屬性值

Int64

正在傳送要求資料的位元組數。The number of bytes of request data being sent.

例外狀況

當屬性在子代類別中未覆寫時,會嘗試取得或設定該屬性。Any attempt is made to get or set the property, when the property is not overridden in a descendant class.

範例

下列範例會將 ContentLength 屬性設定為傳出位元組緩衝區中的位元組數量。The following example sets the ContentLength property to the amount of bytes in the outgoing byte buffer.

// 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();

// 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();



' 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()

備註

ContentLength屬性包含實例傳送至網際網路資源的資料位元組數目 WebRequestThe ContentLength property contains the number of bytes of data sent to the Internet resource by the WebRequest instance.

注意

WebRequest類別是 abstract 類別。The WebRequest class is an abstract class. WebRequest實例在執行時間的實際行為是由方法所傳回的子代類別所決定 WebRequest.CreateThe actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. 如需預設值和例外狀況的詳細資訊,請參閱下階類別的檔,例如 HttpWebRequestFileWebRequestFor more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

適用於

另請參閱