WebRequest.ContentLength 屬性

定義

在子代類別中覆寫時,取得或設定正在傳送要求資料的內容長度。

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

正在傳送要求資料的位元組數。

例外狀況

當屬性在子代類別中未覆寫時,會嘗試取得或設定該屬性。

範例

下列範例會將 ContentLength 屬性設定為傳出位元組緩衝區中的位元組數量。

// 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屬性包含 實例傳送至網際網路資源 WebRequest 的資料位元組數目。

注意

類別 WebRequest 是類別 abstract 。 執行時間實例的實際行為是由 方法傳 WebRequest.Create 回的 WebRequest 子系類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱 子系類別的檔,例如 HttpWebRequestFileWebRequest

適用於

另請參閱