HttpWebResponse.ContentLength プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
要求で返されるコンテンツ長を取得します。
public:
virtual property long ContentLength { long get(); };
public override long ContentLength { get; }
member this.ContentLength : int64
Public Overrides ReadOnly Property ContentLength As Long
プロパティ値
要求で返されるバイト数。 コンテンツ長には、ヘッダー情報は含まれません。
例外
現在のインスタンスは破棄されています。
例
次の例では、このプロパティの値を表示します。
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;
// Specify the URL to receive the request.
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create(args[1]));
// Set some reasonable limits on resources used by this request
request->MaximumAutomaticRedirections = 4;
request->MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request->Credentials = CredentialCache::DefaultCredentials;
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
Console::WriteLine("Content length is {0}", response->ContentLength);
Console::WriteLine("Content type is {0}", response->ContentType);
// Get the stream associated with the response.
Stream^ receiveStream = response->GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader^ readStream = gcnew StreamReader(receiveStream, Encoding::UTF8);
Console::WriteLine("Response stream received.");
Console::WriteLine(readStream->ReadToEnd());
response->Close();
readStream->Close();
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
using System;
using System.Net;
using System.Text;
using System.IO;
public class Test
{
// Specify the URL to receive the request.
public static void Main (string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Content length is {0}", response.ContentLength);
Console.WriteLine("Content type is {0}", response.ContentType);
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine("Response stream received.");
Console.WriteLine(readStream.ReadToEnd());
response.Close();
readStream.Close();
}
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Test
' Specify the URL to receive the request.
Public Shared Sub Main(ByVal args() As String)
Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
' Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4
request.MaximumResponseHeadersLength = 4
' Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Console.WriteLine("Content length is {0}", response.ContentLength)
Console.WriteLine("Content type is {0}", response.ContentType)
' Get the stream associated with the response.
Dim receiveStream As Stream = response.GetResponseStream()
' Pipes the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
Console.WriteLine("Response stream received.")
Console.WriteLine(readStream.ReadToEnd())
response.Close()
readStream.Close()
End Sub
End Class
'
'The output from this example will vary depending on the value passed into Main
'but will be similar to the following:
'
'Content length is 1542
'Content type is text/html; charset=utf-8
'Response stream received.
'...
'
'
注釈
プロパティには ContentLength 、応答で返される Content-Length ヘッダーの値が含まれています。 Content-Length ヘッダーが応答で設定されていない場合、 ContentLength は値 -1 に設定されます。
適用対象
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET