共用方式為


HttpListenerRequest.Headers 屬性

定義

取得在要求中所傳送之標頭名稱/值組的集合。

public:
 property System::Collections::Specialized::NameValueCollection ^ Headers { System::Collections::Specialized::NameValueCollection ^ get(); };
public System.Collections.Specialized.NameValueCollection Headers { get; }
member this.Headers : System.Collections.Specialized.NameValueCollection
Public ReadOnly Property Headers As NameValueCollection

屬性值

WebHeaderCollection,含有要求中所包含的 HTTP 標頭。

範例

下列程式代碼範例會顯示指定 WebHeaderCollection 物件中的所有資訊。

    // Displays the header information that accompanied a request.
public static void DisplayWebHeaderCollection(HttpListenerRequest request)
{
    System.Collections.Specialized.NameValueCollection headers = request.Headers;
    // Get each header and display each value.
    foreach (string key in headers.AllKeys)
    {
        string[] values = headers.GetValues(key);
        if(values.Length > 0)
        {
            Console.WriteLine("The values of the {0} header are: ", key);
            foreach (string value in values)
            {
                Console.WriteLine("   {0}", value);
            }
        }
        else
        {
            Console.WriteLine("There is no value associated with the header.");
        }
    }
}
Public Shared Sub DisplayWebHeaderCollection(ByVal request As HttpListenerRequest)
    Dim headers As System.Collections.Specialized.NameValueCollection = request.Headers

    ' Get each header and display each value.
    For Each key As String In headers.AllKeys
        Dim values As String() = headers.GetValues(key)

        If values.Length > 0 Then
            Console.WriteLine("The values of the {0} header are: ", key)

            For Each value As String In values
                Console.WriteLine("   {0}", value)
            Next
        Else
            Console.WriteLine("There is no value associated with the header.")
        End If
    Next
End Sub

備註

要求標頭包含元數據資訊。 例如,標頭可以包含將客戶端參考至伺服器之資源的統一資源標識碼 (URI) 、用戶端採用的使用者代理程式的身分識別,以及回應本文中數據的可接受的 MIME 類型。

如需要求標頭的完整清單,請參閱 HttpRequestHeader 列舉。

適用於

另請參閱