HttpWebResponse.Headers 屬性

定義

取得與伺服器的這個回應關聯的標頭。

C#
public override System.Net.WebHeaderCollection Headers { get; }

屬性值

WebHeaderCollection,包含隨回應傳回的標頭資訊。

例外狀況

目前的執行個體已經過處置。

範例

下列範例會將所有回應標頭的內容寫入主控台。

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

備註

屬性 Headers 是名稱/值組的集合,其中包含回應傳回的 HTTP 標頭值。 從因特網資源傳回的通用標頭資訊會公開為 類別的屬性 HttpWebResponse 。 下表列出 API 公開為屬性的通用標頭。

標頭 屬性
Content-Encoding ContentEncoding
Content-Length ContentLength
Content-Type ContentType
上次修改時間 LastModified
伺服器 Server

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0