次の方法で共有


HttpWebResponse.Headers プロパティ

応答に関連付けられているヘッダーをサーバーから取得します。

Overrides Public ReadOnly Property Headers As WebHeaderCollection
[C#]
public override WebHeaderCollection Headers {get;}
[C++]
public: __property WebHeaderCollection* get_Headers();
[JScript]
public override function get Headers() : WebHeaderCollection;

プロパティ値

応答で返されるヘッダー情報を格納する WebHeaderCollection

例外

例外の種類 条件
ObjectDisposedException 現在のインスタンスは破棄されています。

解説

Headers プロパティは、応答で返される HTTP ヘッダー値を格納する名前/値ペアのコレクションです。インターネット リソースから返される共通ヘッダー情報は、 HttpWebResponse クラスのプロパティとして公開されます。API がプロパティとして公開する共通ヘッダーの一覧を次の表に示します。

ヘッダー プロパティ
Content-Encoding ContentEncoding
Content-Length ContentLength
Content-Type ContentType
Last-Modified LastModified
Server Server

使用例

[Visual Basic, C#, C++] すべての応答ヘッダーの内容をコンソールに出力する例を次に示します。

 
' Creates an HttpWebRequest with the specified URL. 
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
' Sends the HttpWebRequest and waits for a response.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
' Displays all the Headers present in the response received from the URI.
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "The following headers were received in the response")
'The Headers property is a WebHeaderCollection. Use it's properties to traverse the collection and display each header.
Dim i As Integer
While i < myHttpWebResponse.Headers.Count
    Console.WriteLine(ControlChars.Cr + "Header Name:{0}, Value :{1}", myHttpWebResponse.Headers.Keys(i), myHttpWebResponse.Headers(i))
  i = i + 1
End While
myHttpWebResponse.Close()

[C#] 
// Creates an HttpWebRequest for the specified URL. 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
                        
// Displays all the headers present in the response received from the URI.
Console.WriteLine("\r\nThe following headers were received in the response:");
// Displays each header and it's key associated with the response.
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)  
    Console.WriteLine("\nHeader Name:{0}, Value :{1}",myHttpWebResponse.Headers.Keys[i],myHttpWebResponse.Headers[i]); 
// Releases the resources of the response.
myHttpWebResponse.Close(); 

[C++] 
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest* myHttpWebRequest =
   dynamic_cast<HttpWebRequest*>(WebRequest::Create(url));
// Sends the HttpWebRequest and waits for response.
HttpWebResponse* myHttpWebResponse =
   dynamic_cast<HttpWebResponse*>(myHttpWebRequest->GetResponse());

// Displays all the headers present in the response received from the URI.
Console::WriteLine(S"\r\nThe following headers were received in the response:");
// Displays each header and its key associated with the response.
for (int i=0; i < myHttpWebResponse->Headers->Count; ++i)
   Console::WriteLine(S"\nHeader Name: {0}, Value : {1}",
   myHttpWebResponse->Headers->Keys->Item[i], myHttpWebResponse->Headers->Item[i]);
// Releases the resources of the response.
myHttpWebResponse->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

参照

HttpWebResponse クラス | HttpWebResponse メンバ | System.Net 名前空間