WebResponse.ContentLength 属性

定义

当在子类中重写时,获取或设置接收的数据的内容长度。

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

属性值

Int64

从 Internet 资源返回的字节数。

例外

当未在子类中重写该属性时,试图获取或设置该属性。

示例

以下示例使用 ContentLength 该属性获取返回的资源的长度。

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

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

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

注解

ContentLength 属性包含来自 Internet 资源的响应的长度(以字节为单位)。 对于包含标头信息的请求方法, ContentLength 不包括标头信息的长度。

备注

WebResponse 类是一个 abstract 类。 运行时实例的实际行为 WebResponse 由返回的 WebRequest.GetResponse后代类决定。 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebResponseFileWebResponse

适用于

另请参阅