派生クラスでオーバーライドされると、受信しているデータのコンテンツ長を取得または設定します。
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);
プロパティ値
インターネット リソースから返されるバイト数。
例外
例外の種類 | 条件 |
---|---|
NotSupportedException | プロパティが派生クラスでオーバーライドされていないのに、そのプロパティの取得または設定が試行されました。 |
解説
ContentLength プロパティは、インターネット リソースからの応答のバイト長を格納します。ヘッダー情報を含む要求メソッドでは、 ContentLength にはヘッダー情報の長さは含まれません。
メモ WebResponse クラスは、抽象 (Visual Basic では MustInherit) クラスです。実行時の WebResponse インスタンスの実際の動作は、 WebRequest.GetResponse で返される派生クラスによって決まります。既定値および例外の詳細については、 HttpWebResponse や FileWebResponse などの派生クラスの説明を参照してください。
使用例
[Visual Basic, C#, C++] ContentLength プロパティを使用して、返されたリソースの長さを取得する例を次に示します。
' 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("https://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"https://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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
WebResponse クラス | WebResponse メンバ | System.Net 名前空間 | HttpWebResponse.ContentLength